Doctrine合并删除新的关联/链接

时间:2014-10-03 07:51:15

标签: php doctrine-orm

使用关联/链接更新分离的实体时,我们遇到了一些困难。我们有一个与类别实体有多对多关系的文章实体。

通过表单和提交向现有文章添加类别时,我们会收到以下消息:

  

执行'INSERT INTO篇文章时发生异常...密钥

的重复条目'test'

这是奇怪的,因为实体是现有的(带有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。刷新,更新所有文章数据,但没有链接到第三类,因为它被删除。

$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();

1 个答案:

答案 0 :(得分:0)

  

“在现有文章中添加类别时......”

如果它是存在的文章你不应该 shit 这个:

$model = $entityManager->merge($model);

Doctrine允许您直接更改对象的状态(如果它是通过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();

请注意以下警告:

  

When you want to serialize/unserialize entities you have to make all entity properties protected, never private. The reason for this is, if you serialize a class that was a proxy instance before, the private variables won’t be serialized and a PHP Notice is thrown.