我正在研究一个项目,用ZF2和Doctrine实现ERP Web应用程序。
现在,当我尝试持久化实体时,我遇到了问题。
此消息失败: 可捕获的致命错误:无法将类\ Application \ Entity \ Scope的对象转换为第2791行的\\ vendor \ doctrine \ orm \ lib \ Doctrine \ ORM \ UnitOfWork.php中的字符串
我在这封邮件中发现了类似的问题,但答案并没有帮助我: Doctrine: Object of class User could not be converted to string
我试图保留与 ScopedName 有关系的实体 Product ,并且此实体与 Scope 有关系实体。
当我向 Scope 实体添加__toString()方法时,它会正确保存所有内容,因此我猜测问题不在我的表单或控制器中。如果我的映射没问题,我猜它是一个不正确支持复合主键的Doctrine Bug。如下所示,我的实体 ScopedName 有一个复合键。
这些是我的相关学说映射
范围
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Application\Entity\Scope" table="application_scope">
<id name="id" type="integer">
<generator strategy="AUTO"/>
<sequence-generator sequence-name="tablename_seq" allocation-size="100" initial-value="1" />
</id>
<field name="code" type="string" />
<unique-constraints>
<unique-constraint columns="code" />
</unique-constraints>
</entity>
</doctrine-mapping>
ScopedName
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Catalog\Entity\Product\ScopedName" table="catalog_product_scoped_name">
<id name="scope" column="scope_id" association-key="true" />
<id name="product" column="product_id" association-key="true" />
<field name="value" type="string" />
<many-to-one field="scope" target-entity="Application\Entity\Scope">
<join-column name="scope_id" referenced-column-name="id" on-delete="CASCADE" on-update="CASCADE" nullable="false" />
<cascade>
<cascade-persist />
</cascade>
</many-to-one>
<many-to-one field="product" target-entity="Catalog\Entity\Product" inversed-by="scopedNames" nullable="false">
<join-column name="product_id" referenced-column-name="id" on-delete="CASCADE" on-update="CASCADE" />
<cascade>
<cascade-persist />
</cascade>
</many-to-one>
</entity>
</doctrine-mapping>
更新
是否可能因为我的范围映射中没有一对多?但我在这里只需要一个单向关联,因为我的产品实体将获得更多关联,如 ScopedSku , ScopedDescription ,......我不会&# 39;我想在我的范围映射中将它们全部定义为另一个模块,并且不应该知道所有这些......
更新2
好的,我试图在对方添加一对多,但也没有帮助。有人知道为什么我会收到这个错误吗?
答案 0 :(得分:0)
对自己感到羞耻。这是我自己在控制器中的失败,对不起。
我的editAction看起来像这样......评论的行导致了这个错误
public function editAction() {
$id = $this->params()->fromRoute('id');
$form = $this->getServiceLocator()->get('FormElementManager')->get('\Catalog\Form\ProductForm');
$form->addAdditional();
$product = $this->getEntityManager()->find('Catalog\Entity\Product', $id);
$productOld = clone($product);
$form->bind($product);
if($this->request->isPost()) {
$form->setData($this->request->getPost());
if($form->isValid()) {
foreach($product->getScopedNames() as $scopedName) {
$scopedName->setProduct($product);
//next line was the failure
//$scopedName->setScope($this->getEntityManager()->getReference('Application\Entity\Scope', $scopedName->getScope()));
//$this->getEntityManager()->persist($scopedName);
}
foreach($productOld->getScopedNames() as $scopedName) {
if(!$product->getScopedNames()->contains($scopedName)) {
$this->getEntityManager()->remove($scopedName);
}
}
$this->getEntityManager()->persist($product);
$this->getEntityManager()->flush();
$this->flashMessenger()->addSuccessMessage("Product has been saved successfully.");
$this->redirect()->toRoute('catalog/product');
} else {
$this->flashMessenger()->addErrorMessage("Form invalid");
}
} else {
$form->bind($product);
}
return array(
'form' => $form,
'id' => $id
);
}
在使用 DoctrineObject Hydrator之前,我需要这些行。现在它可以自动运行。但我仍然需要前一行和下一个foreach,以便将产品本身设置为关系,并删除新帖子请求中未包含的旧关系。
也许某人有更好的解决方案呢?