我正在研究一个项目,用ZF2和Doctrine实现一个ERP网络应用程序。
现在,当我试图坚持一个实体时,我遇到了一个问题。
此消息失败:...\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php可捕获致命错误:类的对象\Entity\Scope无法在第2791行的中转换为字符串
我在这个消息中发现了一个类似的问题,但答案并没有帮助我: Doctrine: Object of class User could not be converted to string
我试图持久化实体产品,它与ScopedName有关系,而这个实体与范围实体有关系。
当我向Scope实体添加__toString()方法时,它正确地保存了所有内容,因此我猜问题不在我的表单或控制器中。如果我的映射是好的,我猜是一个Doctrine不支持正确的复合主键。如下面所示,我的实体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,.我不想在我的范围映射中把它们都定义为在另一个模块中,并且不应该知道所有这些.
更新2
好吧,我试着把一对多加到另一边,但也没帮上忙。有人知道我为什么会犯这个错误吗?
发布于 2014-11-04 21:37:18
以我自己为耻。这是我自己在控制器上的失误,抱歉。
我的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水化器之前,我需要这些线。现在它自动工作了。但我仍然需要行之前和下一个预见,以设置产品本身的关系,并删除旧的关系,不包含在新的帖子请求。
也许有人对此也有更好的解决方案?
https://stackoverflow.com/questions/26741772
复制相似问题