我们在更新具有关联/链接的分离实体时遇到了一些困难。我们有一个与类别实体具有多对多关系的文章实体。
当通过表单向现有文章添加类别并提交时,我们会收到以下消息:
执行"INSERT INTO项目…"时发生异常。。。密钥的重复条目"测试"
这很奇怪,因为实体是一个现有的实体(带有id),为什么要使用insert?
由于文章实体是分离/序列化的,我们试图将其合并,这次我们没有收到任何错误,但没有将新的类别链接插入数据库。我们统计了合并前后的类别,发现合并时它们会重置为数据库状态,所以如果它最初有2个类别,我们添加了第三个类别,则合并前和合并后的计数分别为3和2。新类别也是一个仅包含类别id的分部实体。
这是代码:
$connection->beginTransaction();
try {
// Manage model by doctrine if it's not.
$modelState = $entityManager->getUnitOfWork()->getEntityState($model);
if ($modelState !== UnitOfWork::STATE_MANAGED) {
$model = $entityManager->merge($model); // Strips new categories.
}
$entityManager->persist($model);
$entityManager->flush();
$connection->commit();
} catch ('Exception $e) {
$connection->rollback();
}
实体:
<entity name="Article" table="articles" repository-class="Doctrine'Repository'BaseRepository">
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="title" type="string" column="title" length="50" nullable="true"/>
<many-to-many field="categories" target-entity="ArticleCategory" inversed-by="articles">
<cascade>
<cascade-persist/>
</cascade>
<join-table name="article_category_links">
<join-columns>
<join-column name="article_id" referenced-column-name="id" on-delete="CASCADE"/>
</join-columns>
<inverse-join-columns>
<join-column name="article_category_id" referenced-column-name="id" on-delete="CASCADE"/>
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
<entity name="ArticleCategory" table="article_categories" repository-class="Doctrine'Repository'BaseRepository">
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="name" type="string" column="name" length="200" nullable="true"/>
<many-to-many field="articles" target-entity="Article" mapped-by="categories">
<cascade>
<cascade-persist/>
</cascade>
</many-to-many>
</entity>
更新
这就是我们正在做的:
1.表单$_POST数据,第三类是新的:
Array
(
[id] => 1
[title] => test
[categories] => Array
(
[0] => Array
(
[id] => 1
)
[1] => Array
(
[id] => 2
)
[2] => Array
(
[id] => 3
)
)
)
2.反序列化的文章(JMS''Serializer):
Article Object
(
[id:protected] => 1
[title:protected] => test
[categories:protected] => Array
(
[0] => ArticleCategory Object
(
[id:protected] => 1
[name:protected] =>
[articles:protected] =>
)
[1] => ArticleCategory Object
(
[id:protected] => 2
[name:protected] =>
[articles:protected] =>
)
[2] => ArticleCategory Object
(
[id:protected] => 3
[name:protected] =>
[articles:protected] =>
)
)
)
3.合并分离/反序列化的项目,删除第三类
$article = $entityManager->merge($article);
4.Flush,更新所有文章数据,但没有链接到第三类,因为它被删除了
$entityManager->flush();
以下是更多示例/测试:
// With attached article and no merging.
// Creates the link to the article, but the category becomes a new one (new id)..
$article = $articleRepository->getById(1)->getItem();
$category = new ArticleCategory();
$category->setId(3); // This doesnt matter since it becomes a new one.
$article->addCategory($category);
$articleRepository->getEntityManager->flush();
// With attached article and merging of category.
// Creates the link to the article but it clears all the category fields..
$article = $articleRepository->getById(1)->getItem();
$category = new ArticleCategory();
$category->setId(3);
$category = $categoryRepository->getEntityManager->merge($category);
$article->addCategory($category);
$articleRepository->getEntityManager->flush();
// With detached and merged article.
// No links no errors, but other article fields gets updated.
$category = new ArticleCategory();
$category->setId(3);
$article->addCategory($category);
echo count($article->getCategories()); // 3
$article = $articleRepository->getEntityManager->merge($article);
echo count($article->getCategories()); // 2, the new one gets removed.
$articleRepository->getEntityManager->flush();
"向现有文章添加类别时…"
如果这是一篇存在的文章,你不应该做大便这个:
$model = $entityManager->merge($model);
如果对象是通过EntityManager加载的,则原则允许您直接更改对象的状态。
"这很奇怪,因为实体是一个现有的实体(有id),为什么它使用插入?"
因为您正在调用persist()方法:
$entityManager->persist($model);
试试这样的东西:
$cat1 = $entityManager->find("ArticleCategory", 1);
$cat2 = $entityManager->find("ArticleCategory", 2);
$model = $entityManager->find("Article", $id);
$model->getCategories()->add($cat1);
$model->getCategories()->add($cat2);
// because the $model exists, you only need to syncronize the changes in the db
$entityManager->flush();
更新
在你需要再次取消一个模型的序列化时,这应该做到:
$detachedEntity = unserialize($model); // some detached entity
$entity = $entityManager->merge($detachedEntity);
// and then, sync the changes in db
$entityManager->flush();
注意以下警告:
当您想要序列化/取消序列化实体时,必须保护所有实体属性,而不是私有属性。原因是,如果您序列化一个以前是代理实例的类,则私有变量将不会被序列化,并引发PHP通知。