php - Doctrine 实体和特征。正确的方法

标签 php symfony doctrine-orm doctrine traits

我有一个评论实体(用于用户评论),我想在我的旧实体中添加一个新功能(可评论)。 我创建了一个可评论的特征:

trait Commentable
{
    /**
     * List of comments
     *
     * @var Comment[]|ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="Comment")
     */
    protected $comments;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->comments = new ArrayCollection();
    }

    /**
     * Get Comments
     *
     * @return Comment[]|ArrayCollection
     */
    public function getComments()
    {
        return $this->comments;
    }

    /**
     * Add comment to the entity
     *
     * @param Comment $comment
     */
    public function addComment(Comment $comment)
    {
        $this->comments->add($comment);
    }
}

在旧的实体中我做这样的事情:

class Image
{
    use Commentable {
        Commentable::__construct as private __commentableConstruct;
    }

    /** some stuff **/
}

评论类看起来像:

class Comment
{
    /**
     * Identifier
     *
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * Comment owner
     *
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="User", inversedBy="comments")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    /**
     * Comment content
     *
     * @var string
     *
     * @ORM\Column(type="text")
     */
    protected $content;

    /**
     * @var Image
     *
     * @ORM\ManyToOne(targetEntity="Image", inversedBy="comments")
     */
    protected $image;
    /** all the classes using Commentable **/

    /** some stuff */
}

我觉得这个想法不错。我可以创建新的行为并将其轻松添加到实体中。 但我不喜欢 Comment 实体的想法。使用可评论特征添加所有类不是“有用的”。 我收到此错误...但我不知道如何使用 traits 修复它:

OneToMany mapping on field 'comments' requires the 'mappedBy' attribute.

最佳答案

我用

解决了这个问题
trait Commentable
{
    /**
     * List of comments
     *
     * @var Comment[]|ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="XXXX\Entity\Comment")
     * @ORM\OrderBy({"createdAt" = "DESC"})
     */
    protected $comments;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->comments = new ArrayCollection();
    }

    /**
     * Get Comments
     *
     * @return Comment[]|ArrayCollection
     */
    public function getComments()
    {
        return $this->comments;
    }

    /**
     * Add comment to the entity
     *
     * @param Comment $comment
     */
    public function addComment(Comment $comment)
    {
        $this->comments->add($comment);
    }
}

关于php - Doctrine 实体和特征。正确的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17695328/

相关文章:

php - MySQL 选择查询

php - 用户ID动态图片

php - 未指定输入文件。 Laravel 5.5 CentO

Doctrine 2 : how to clone all values from one object onto another except ID?

symfony - Symfony2,以微秒为单位的学说日期时间

php - Symfony 和 Twig 模板。如何动态扩展模板或什么也不扩展?

php - 单表实体生成

mongodb - 使用 DoctrineMongoDBBundle 进行身份验证

php - Symfony2 + Doctrine2 SQL 完整性违规 : Nullable field can't be null?

symfony4 在服务中使用 .env 配置变量