Orm 带ZF2的条令2如何更新行
我正在尝试使用以下命令更新实体中的行:Orm 带ZF2的条令2如何更新行,orm,doctrine-orm,zend-framework2,Orm,Doctrine Orm,Zend Framework2,我正在尝试使用以下命令更新实体中的行: $linker = $this->getObjectManager()->getRepository('\Schema\Entity\Link')->find('link_id', 7853); $linker->setSampleTitle($mytitle); $linker->setSampleDesc($mydesc); $this->getOb
$linker = $this->getObjectManager()->getRepository('\Schema\Entity\Link')->find('link_id', 7853);
$linker->setSampleTitle($mytitle);
$linker->setSampleDesc($mydesc);
$this->getObjectManager()->merge($linker);
$this->getObjectManager()->flush();
但是我得到:此操作需要打开事务。尝试:
$this->getObjectManager()->persist($linker);
$this->getObjectManager()->flush();
此外,您可能希望使用findOneBy而不是find。尝试以下方法:
$this->getObjectManager()->persist($linker);
$this->getObjectManager()->flush();
另外,您可能希望使用findOneBy而不是find。我刚刚意识到,在
find()
方法中,我不应该放置'link\u id'
,而应该放置类\Schema\Entity\link
。所以这里不需要getRepository()
正确的是:
$linker=$this->getObjectManager()->find('\Schema\Entity\Link',7853)
我刚刚意识到,在find()
方法中,我不应该放置'link\u id'
,而应该放置类\Schema\Entity\link
。所以这里不需要getRepository()
正确的是:$linker=$this->getObjectManager()->find('\Schema\Entity\Link',7853)代码>
并通过@id将link\u id
定义为实体中的id,或使用findBy
关键字
并通过@id将link\u id
定义为实体中的id,或者使用findBy
关键字实际上您的find()
方法都是正确的。然而,第一个“查询表”,第二个“查询数据库”——也就是说。。。正如@doeni所指出的,您只是使用了错误的函数来保存更新的实体。你要找的是persist()
而不是merge()
Hi,事实是我完全删除了$this->getObjectManager()->merge($linker);使用我在下面发布的方法,它既不使用persist
也不使用merge
。我只使用了merge
,因为我在另一篇文章中读到了它,而不知道它到底是做什么的。是否需要使用persist()
?是的,我自己也忘记了这一点。更新只需要刷新。。。不需要持久化,因为在编辑的情况下,这都是内部处理的。flush()将把它写入数据库实际上您的find()
方法都是正确的。然而,第一个“查询表”,第二个“查询数据库”——也就是说。。。正如@doeni所指出的,您只是使用了错误的函数来保存更新的实体。你要找的是persist()
而不是merge()
Hi,事实是我完全删除了$this->getObjectManager()->merge($linker);使用我在下面发布的方法,它既不使用persist
也不使用merge
。我只使用了merge
,因为我在另一篇文章中读到了它,而不知道它到底是做什么的。是否需要使用persist()
?是的,我自己也忘记了这一点。更新只需要刷新。。。不需要持久化,因为在编辑的情况下,这都是内部处理的。flush()将其写入数据库