2

I am getting "Notice: Array to string conversion" errors since I changed a field in my table. I think I made a mistake in changing my entity (which was an integer before)

  /**
     * @var array
     * @ORM\Column(type="array", nullable=false)
     */
     protected $sector = array();
.
.
.
.

 /**
 * Set sector
 *
 * @param array $sector
 * @return Promotion
 */
public function setSector($sector)
{
    $this->sector = $sector;

    return $this;
}

/**
 * Get sector
 *
 * @return array
 */
public function getSector()
{
    return $this->sector;
}

                  

Stack Trace:

[1] Doctrine\DBAL\DBALException: An exception occurred while executing 'INSERT INTO promotions (articleid, name1, name2, suppliername, picture, datefrom, dateto, position, highlight, category, sector, adminid, entrydate, alterationdate, description, aktion_id, alternative, alternativetext) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["1234567", "IFM N\u00e4herungsschalter kapazitiv KI5023", null, "IFM", "image_5_1234567.jpg", "2015-04-15", "2015-05-22", 1, 2, "75", [2,4], 12429, "2015-05-04 09:47:20", "2015-05-04 09:47:20", "<p>test<\/p>", 1, null, null]:

Notice: Array to string conversion in /server/xyz/Symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 138
    at n/a
        in /server/xyz/Symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php line 47

    at Doctrine\DBAL\DBALException::driverExceptionDuringQuery(object(ContextErrorException), 'INSERT INTO promotions (articleid, name1, name2, suppliername, picture, datefrom, dateto, position, highlight, category, sector, adminid, entrydate, alterationdate, description, aktion_id, alternative, alternativetext) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', array('1234567', 'IFM Näherungsschalter kapazitiv KI5023', null, 'IFM', 'image_5_1234567.jpg', '2015-04-15', '2015-05-22', '1', '2', '75', array('2', '4'), '12429', '2015-05-04 09:47:20', '2015-05-04 09:47:20', '&lt;p&gt;test&lt;/p&gt;', '1', null, null))
        in /server/xyz/Symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 140

    at Doctrine\DBAL\Statement->execute()
        in /server/xyz/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 277

    at Doctrine\ORM\Persisters\BasicEntityPersister->executeInserts()
        in /server/xyz/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 929

    at Doctrine\ORM\UnitOfWork->executeInserts(object(ClassMetadata))
        in /server/xyz/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 318

    at Doctrine\ORM\UnitOfWork->commit(null)
        in /server/xyz/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php line 355

    at Doctrine\ORM\EntityManager->flush()
        in /server/xyz/Symfony/src/xyz/ShopBundle/Controller/OfferWeekController.php line 305

    at xyz\ShopBundle\Controller\OfferWeekController->offerweekAction(object(Request), '0')
        in  line 

    at call_user_func_array(array(object(OfferWeekController), 'offerweekAction'), array(object(Request), '0'))
        in /server/xyz/Symfony/app/bootstrap.php.cache line 2770

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in /server/xyz/Symfony/app/bootstrap.php.cache line 2744

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in /server/xyz/Symfony/app/bootstrap.php.cache line 2874

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in /server/xyz/Symfony/app/bootstrap.php.cache line 2175

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in /server/xyz/Symfony/web/app_dev.php line 30

[2] Symfony\Component\Debug\Exception\ContextErrorException: Notice: Array to string conversion in /server/xyz/Symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 138
    at n/a
        in /server/xyz/Symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 138

    at Symfony\Component\Debug\ErrorHandler->handle('8', 'Array to string conversion', '/server/xyz/Symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php', '138', array('params' => null, 'logger' => object(LoggerChain)))
        in  line 

    at PDOStatement->execute(null)
        in /server/xyz/Symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 138

    at Doctrine\DBAL\Statement->execute()
        in /server/xyz/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 277

    at Doctrine\ORM\Persisters\BasicEntityPersister->executeInserts()
        in /server/xyz/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 929

    at Doctrine\ORM\UnitOfWork->executeInserts(object(ClassMetadata))
        in /server/xyz/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 318

    at Doctrine\ORM\UnitOfWork->commit(null)
        in /server/xyz/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php line 355

    at Doctrine\ORM\EntityManager->flush()
        in /server/xyz/Symfony/src/xyz/ShopBundle/Controller/OfferWeekController.php line 305

    at xyz\ShopBundle\Controller\OfferWeekController->offerweekAction(object(Request), '0')
        in  line 

    at call_user_func_array(array(object(OfferWeekController), 'offerweekAction'), array(object(Request), '0'))
        in /server/xyz/Symfony/app/bootstrap.php.cache line 2770

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in /server/xyz/Symfony/app/bootstrap.php.cache line 2744

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in /server/xyz/Symfony/app/bootstrap.php.cache line 2874

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in /server/xyz/Symfony/app/bootstrap.php.cache line 2175

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in /server/xyz/Symfony/web/app_dev.php line 30

Thanks in advance for your help.

8
  • So why did you need to change the type of the sector column? It was used to persist a sector number but now you make that value an array instead of a number in your PHP code. Commented May 4, 2015 at 8:36
  • @Burnt could you check the type of your column since you updated the field column and ran the doc:schema:update command? By the way, I would suggest you to use symfony.com/fr/doc/current/bundles/DoctrineMigrationsBundle/… since it helps about migrating/reverting easily. Commented May 4, 2015 at 8:59
  • @Marcell: i need to map articls to sectors of our enterprise (mostly to all 4). My predecessor saved each article for each sector, which seemed redundant to me. So I tried to save the sectors as an arry (my guess is, "simple_array" would be sufficient) Commented May 4, 2015 at 9:00
  • @Ninir: Table was altered correctly, field is now "longtext" with comment Commented May 4, 2015 at 9:05
  • 1
    Could you run php app/console doctrine:schema:validate to be sure that the mapping is OK, and then dump $sector in your setSector please? Commented May 4, 2015 at 9:09

3 Answers 3

1

array column type do exists.

But Doctrine store a comment in your database. This comments helps him to know what to do with your data.

I think you can run the php app/console doctrine:shema:update --dump-sql command. It will propose you an ALTER command that you have to run.

After that, it should works fine ;)

Sign up to request clarification or add additional context in comments.

10 Comments

Already did what you proposed, it still doesnt work (it altered the field to "longtext" an added a comment) sorry, posting comments here is kinda confusing
What $sectors do you inject in setSector ? Is it [2, 4] or something else ?
the dump of my form input looks like this: array(2) { [0]=> int(2) [1]=> int(4) }, so yes, what i inject should look like what you wrote
Well that's weird. Maybe you ccan use the simple_array type or the json_array to see if it's better (the simple_array seems fine for me as you insert just unindexed integers)
Thanks a lot for the simple_array remark, it's working now. I do have a follow-up question though: I need to change a query, where i check if an integer (Parameter1, sector from customer data) is in my sector-array. before, it looked like this: $qb->andWhere('p.sector = ?1'); $qb->setParameter(1, $this->contact->getCompanyarea()); is $qb->andWhere('?1 in (p.sector)'; the correct way?
|
0

To store an array in the database, convert into a json encoded string and then store in the array. So, modify the getter and setter like this

 /**
 * Set sector
 *
 * @param array $sector
 * @return Promotion
 */
 public function setSector($sector)
 {
    $this->sector = json_encode($sector);

   return $this;
 }

/**
 * Get sector
 *
 * @return array
 */
 public function getSector()
 {
   if($this->sector){
      return json_decode($sector);
  }    
  return null;
 }

4 Comments

Your code should not handle this as Doctrine is already doing it for your since you can define 3 types: array, simple_array, json_array.
I also highly suggest not to polute the entity classes with json_encode and json_decode.
@tomazahlin mind elaborating why json encode and decode pollutes the entity class?
@Dheeraj, entity classes "should" only be as simple as possible, ideally only used to hold the data. Mostly they should contain properties, along with their getter/setter methods. If you need to do some json encoding in your entity classes, you are violating single responsibility principle. But it really depends on your case. If this is the simplest and quickest solution, which saves you a lot of effort and which works, why not use it. ;)
0

My situation was different, but resulted in a similar error. I got it in a SQL query when trying to limit my answer to just one using '->setMaxResults()->getResult();`.

Turns out getResult() needed to be getSingleResult().

It was returning an array of Entity objects, rather than just the one Entity object I wanted. Using getSingleResult() fixed the error for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.