symfony2 + 教义 : modify a child entity on `onFlush` : "Invalid parameter number: number of bound variables does not match number of tokens"

标签 symfony doctrine-orm

我有一个 1:m Subitem 之间的关系和 SubitemColor .现在我想在 onFlush 中保存一些数据修改 SubitemColor 的一些数据.问题:执行 Controller 时,我收到以下错误消息,您也可以在下面看到:

An exception occurred while executing 'INSERT INTO SubitemColor (code, precio, pvp_recommended, file_name, activado, en_stock, area, lets_fix_width_or_height_in_list, lets_fix_width_or_height_in_show, position_level_0, position_level_1, position_brand, subitem_id, color_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [2]:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens


  public function onFlush(Event \OnFlushEventArgs $eventArgs)
  {
    $em = $eventArgs->getEntityManager();
    $uow = $em->getUnitOfWork();

    $updates = $uow->getScheduledEntityUpdates();
    $insertions = $uow->getScheduledEntityInsertions();

    /////////// NEW SUBITEM_IMAGE OR SUBITEM_COLOR UPLOADED //////////
    foreach ($insertions as $entity) {


      if ($entity instanceof SubitemColor) {
        //$entity->setLetsFixWidthOrHeightInList("jander");
        //$entity->setLetsFixWidthOrHeightInList('width');
        //$entity->setLetsFixWidthOrHeightInShow('width');

        $entity->setEnStock(2);

        $metaSubitemColor = $em->getClassMetadata(get_class($entity));

        $uow->computeChangeSet($metaSubitemColor, $entity);

        $uow->persist($entity);
      }
    }
  }

//controller - controller - controller - controller
    $subitem = new Subitem();

    $em = $this->getDoctrine()->getManager();

    $subitem->setNombre("jls");
    $subitemColor = new SubitemColor();
    $subitem->addSubitemColor($subitemColor);

    $em->persist($subitem);
    $em->persist($subitemColor);

    $metaSubitem = $em->getClassMetadata(get_class($subitem));
    $em->flush();

最佳答案

使用recomputeSingleEntityChangeSet方法而不是 computeChangeSetcomputeChangeSet方法应该仅由学说调用,并为每个标记为在刷新操作中保持持久性的实体调用一次。

当您从数据库中加载实体时,原则将其数据保存到 originalEntityData数组,然后检查实体是否不存在原始数据,然后该实体是新的,并且学说将其当前数据保存为原始数据,并用每个字段值填充更改集。

第二次调用computeChangeSet学说具有新创建实体的原始数据,并且仅计算自上次调用 computeChangeSet 以来更改的字段的更改集方法。

这就是为什么你不应该调用 computeChangeSet .

关于symfony2 + 教义 : modify a child entity on `onFlush` : "Invalid parameter number: number of bound variables does not match number of tokens",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31144447/

相关文章:

php - Symfony2 Doctrine2,是否有 'REPLACE INTO' 函数?

Symfony2 学说标准

php - Doctrine2 生成覆盖 SQL 默认值的 INSERT 语句

php - Symfony 命令 --no-interaction 不起作用

events - Symfony2 : PrePersist/PreUpdate lifecycle-event not fired

php - 在类 ObjectRepository 中找不到方法 'QueryBuilder'

api - 配置 Silex 防火墙使用 api key 的正确方法

Symfony 2 - 翻译不起作用 (xliff)

php - Symfony2,认证后切换到HTTPS

php - Doctrine 2 如何正确映射多级继承