注意:数组转换为字符串,Symfony

时间:2015-05-04 08:32:49

标签: symfony doctrine-orm

我得到"注意:数组到字符串转换"自从我更改了表格中的字段后出现错误。我认为我在改变我的实体(之前是一个整数)时犯了一个错误



  /**
     * @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;
}

                  




堆栈追踪:



[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
&#13;
&#13;
&#13;

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

array列类型确实存在。

但是Doctrine会在您的数据库中存储注释。这些评论有助于他了解如何处理您的数据。

我认为您可以运行php app/console doctrine:shema:update --dump-sql命令。它将为您提供一个必须运行的ALTER命令。

之后,它应该可以正常工作;)

答案 1 :(得分:0)

要在数据库中存储数组,请转换为json编码的字符串,然后存储在数组中。所以,像这样修改getter和setter

 /**
 * 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;
 }

答案 2 :(得分:0)

我的情况不同,但导致了类似的错误。当我尝试使用' - &gt; setMaxResults() - &gt; getResult();`。

将我的答案限制为只有一个时,我在SQL查询中得到了它

原来getResult()需要getSingleResult()

它返回了一个Entity个对象数组,而不仅仅是我想要的一个Entity个对象。使用getSingleResult()为我修复了错误。