symfony 2中带有doctrine 2的嵌套连接joincolumns

时间:2014-05-26 02:16:01

标签: php symfony doctrine-orm

所以给定实体评论,让CommentPerson找出CommentPerson isModerator()的最简洁方法是什么?

使用存储库时不要弄脏手指。

/**
 * @ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\CommentRepository")
 * @ORM\Table(name="comment")
 */
class Comment
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer")
     */
    protected $user_id; //id of entity User

      /**
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     **/
    protected $user; //a user can have many comments


    /**
     * @ORM\Column(type="text")
     */
    protected $commentText;

}


/**
 * @ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\UserRepository")
 * @ORM\Table(name="user")
 */
class User //this is the web user
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer")
     */
    protected $person_id; //id of entity Person

      /**
     * @ORM\ManyToOne(targetEntity="Person")
     * @ORM\JoinColumn(name="person_id", referencedColumnName="id")
     **/
    protected $person; //many web users can map to one actual person


    public function getPerson()
    {
        return $this->person;
    }
}

/**
 * @ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\PersonRepository")
 * @ORM\Table(name="person")
 */
class Person
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="text")
     */
    protected $email;

    /**
     * @ORM\OneToMany(targetEntity="CommentPerson", mappedBy="person")
     * @ORM\JoinColumn(name="id", referencedColumnName="person_id")
     */
    private $commentPersons; //one person can have many commentPersons (since one person can be part in many different comment sections, not shown in code here, but take it as a fact)

}


/**
 * @ORM\Entity(repositoryClass="MyApp\MyBundle\Repository\CommentPersonRepository")
 * @ORM\Table(name="comment_Person")
 */
class CommentPerson
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer")
     */
    protected $person_id; //this is the id of the entity Person

    /**
     * @ORM\Column(type="boolean")
     */
    protected $isModerator;

}

0 个答案:

没有答案