symfony2 +学说:在“ onFlush”上修改子实体:“无效参数号:绑定变量数与令牌数不匹配” [英] symfony2 + doctrine: modify a child entity on `onFlush`: "Invalid parameter number: number of bound variables does not match number of tokens"

查看:85
本文介绍了symfony2 +学说:在“ onFlush”上修改子实体:“无效参数号:绑定变量数与令牌数不匹配”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Subitem SubitemColor 1:m 关系$ c>。现在,我想在 onFlush 中保存一些数据,以修改 SubitemColor 的一些数据。问题:执行控制器时,我也收到以下错误消息:

I have a 1:m relationship between Subitem and SubitemColor. Now I would like to save some data inside an onFlush to modify some data for SubitemColor. The problem: I get the error message below when executing the controller you can see below too:


执行'INSERT INTO SubitemColor'时发生了异常
(代码,precio,pvp_recommended,file_name,activado,en_stock,区域,
lets_fix_width_or_height_in_list,letes_fix_width_or_height_in_show,
position_level_0,position_level_1,position_brand,subitem_id,
color? ,?,?,?,?,?,?,?,?,?,?,?,?,?)'和
参数[2]:

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]:无效的参数编号:绑定变量的数量
与令牌的数量不匹配

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 方法而不是 computeChangeSet

computeChangeSet 方法仅应由教义调用,并且对标记为刷新操作具有持久性的每个实体调用一次。

computeChangeSet method is supposed to be called by doctrine only and calls once for every entity that marked for persistence on flush operation.

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

When you load entity from database doctrine saves its data to originalEntityData array, then it checks if no original data exists for entity then this entity is new and doctrine saves its current data as original and fill change set with every field value.

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

On second call of computeChangeSet doctrine has original data for newly created entity and computes change set only for changed fields since last call of computeChangeSet method.

这就是为什么您永远不要调用 computeChangeSet

Thats why you should never call computeChangeSet.

这篇关于symfony2 +学说:在“ onFlush”上修改子实体:“无效参数号:绑定变量数与令牌数不匹配”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆