2

我在 Symfony2 项目中使用 Doctrine+JMSserializer,刚刚发现在典型的 JMS“反序列化”和 Doctrine“持久化”操作期间相关实体及其数据可能会受到损害的问题。

$data = [
    'locale' => 'en',
    'name' => $this->faker->sentence(),
    'content' => $this->faker->text(),
    'subject' => [
        'id' => $page->getId(),
        'name' => 'new name' // <-- compromised property 
    ]
];
$rawPayload = json_encode($data);

$entity = $this->serializer->deserialize(
    $rawPayload,
    $resolvedClass,
    'json'
);

并且在对新的 $entity 相关的 Page 实体名称进行典型的持久化操作之后,正在更改 - 所以这是一个问题。

$this->getEntityManager()->persist($entity);
$this->getEntityManager()->flush();

目标是为评论设置页面 ID,但防止更改其他属性,否则页面实体可能会受到损害。我试图通过更改注释、学说设置等来解决这个问题。任何想法如何以自然的方式解决这个问题?

评论和页面实体:

use Aisel\ReviewBundle\Entity\Review as BaseReview;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use JMS\Serializer\Annotation as JMS;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Review
 *
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Table(name="aisel_page_review")
 * @ORM\Entity(repositoryClass="Aisel\ResourceBundle\Repository\CollectionRepository")
 * @JMS\ExclusionPolicy("all")
 */
class Review extends BaseReview
{

    /**
     * @var Page
     * @Assert\NotNull()
     * @ORM\ManyToOne(targetEntity="Aisel\PageBundle\Entity\Page", inversedBy="pages")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="page_id", referencedColumnName="id", nullable=false)
     * })
     * @JMS\Type("Aisel\PageBundle\Entity\Page")
     * @JMS\Expose
     * @JMS\MaxDepth(2)
     */
    private $subject;

    /**
     * @return Page
     */
    public function getSubject()
    {
        return $this->subject;
    }

    /**
     * @param Page $subject
     */
    public function setSubject($subject)
    {
        $this->subject = $subject;
    }
}

<?php

namespace Aisel\PageBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use JMS\Serializer\Annotation as JMS;
use Aisel\ResourceBundle\Domain\UrlInterface;
use Aisel\PageBundle\Entity\Node;
use Aisel\PageBundle\Entity\Review;
use Aisel\UserBundle\Entity\User;

use Aisel\ResourceBundle\Domain\IdTrait;
use Aisel\ResourceBundle\Domain\UpdateCreateTrait;
use Aisel\ResourceBundle\Domain\MetaTrait;
use Aisel\ResourceBundle\Domain\LocaleTrait;
use Aisel\ResourceBundle\Domain\StatusTrait;
use Aisel\ResourceBundle\Domain\NameTrait;
use Aisel\ResourceBundle\Domain\ContentTrait;
use Aisel\ResourceBundle\Domain\CommentStatusTrait;

/**
 * Page
 *
 * @author Ivan Proskuryakov <volgodark@gmail.com>
 *
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Table(name="aisel_page")
 * @ORM\Entity(repositoryClass="Aisel\ResourceBundle\Repository\CollectionRepository")
 * @JMS\ExclusionPolicy("all")
 */

class Page implements UrlInterface
{
    use IdTrait;
    use NameTrait;
    use ContentTrait;
    use LocaleTrait;
    use StatusTrait;
    use CommentStatusTrait;
    use MetaTrait;
    use UpdateCreateTrait;

    /**
     * @var ArrayCollection
     * @ORM\ManyToMany(targetEntity="Aisel\PageBundle\Entity\Node")
     * @ORM\JoinTable(
     *     name="aisel_page_page_node",
     *     joinColumns={@ORM\JoinColumn(name="page_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="node_id", referencedColumnName="id")}
     * )
     * @JMS\Type("ArrayCollection<Aisel\PageBundle\Entity\Node>")
     * @JMS\Expose
     * @JMS\MaxDepth(2)
     * @JMS\Groups({"collection","details"})
     */
    private $nodes;

    /**
     * @var ArrayCollection<Aisel\PageBundle\Entity\Review>
     * @ORM\OneToMany(targetEntity="Aisel\PageBundle\Entity\Review", mappedBy="subject", cascade={"remove"})
     * @ORM\OrderBy({"createdAt" = "DESC"})
     * @JMS\Expose
     * @JMS\MaxDepth(2)
     * @JMS\Type("ArrayCollection<Aisel\PageBundle\Entity\Review>")
     * @JMS\Groups({"collection","details"})
     */
    private $reviews;
4

0 回答 0