doctrine 2, how do get data from the inverse side (many to one)

左心房为你撑大大i 提交于 2020-01-16 09:04:32

问题


I have two entities, entry and comments.

comments:

/**
 * @Entity(repositoryClass="\Entities\Blog\CommentRepository")
 * @Table(name="blog_comment")
 * @HasLifecycleCallbacks
 */
class Comment extends \Entities\AbstractEntity
{
    /**
     * @Id @Column(name="id", type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ManyToOne(targetEntity="Entry", inversedBy="comments")
     * @JoinColumn(name="entry_id", referencedColumnName="id")
     */
    protected $entry;

    /** @Column(name="approved", type="string", length=255) */
    protected $approved;

    /** @Column(name="title", type="string", length=255) */
    protected $title;

    /** @Column(name="content", type="text") */
    protected $content;

    /** @Column(name="pub_date", type="datetime") */
    protected $pub_date;

    /** @Column(type="datetime") */
    private $created_at;

    /** @Column(type="datetime") */
    private $updated_at;

    /** @PreUpdate */
    public function updated()
    {
        $this->updated_at = new \DateTime("now");
    }

    public function __construct()
    {
        $this->created_at = $this->updated_at = new \DateTime("now");
    }
}

class CommentRepository extends \Entities\PaginatedRepository
{
    protected $_entityClassName = 'Entities\Blog\Comment';
}

and entry:

<?php
namespace Entities\Blog;

/**
 * @Entity(repositoryClass="\Entities\Blog\EntryRepository")
 * @Table(name="blog_entry")
 * @HasLifecycleCallbacks
 */
class Entry extends \Entities\AbstractEntity
{
    /**
     * @Id @Column(name="id", type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** @Column(name="permalink", type="string", length=255) */
    protected $permalink;

    /** @Column(name="title", type="string", length=255) */
    protected $title;

    /** @Column(name="pub_date", type="datetime") */
    protected $pub_date;

    /** @Column(name="content", type="text") */
    protected $content;

    /** @OneToMany(targetEntity="Comment", mappedBy="entry") */
    protected $comments;

    /** @Column(type="datetime") */
    private $created_at;

    /** @Column(type="datetime") */
    private $updated_at;

    /** @PreUpdate */
    public function updated()
    {
        $this->updated_at = new \DateTime("now");
    }

    public function __construct()
    {
        $this->comments = new \Doctrine\Common\Collections\ArrayCollection();
    }

I can get the collection of all comments belonging to each entry via:

foreach ($comments as $comment){
   $comment-$commentId;
}

but how can I get the entry information from the comments side. for example, I would like to get the entry id from a specific comment


回答1:


Each time you create a @OneToMany relation, you create a Collection of proxy objects in class on "One"-side of relation, and single proxy object in class on "Many"-side of relation. Proxy classes are automatically generated by Doctrine2 from your mapping information.

To allow Doctrine2 filling proxy object with real data from DB it's important to declare it protected or private. I'm not sure about that, but seems like Doctrine tracks down any requests to proxy objects inside your entity class and ensures that proxies are populated before first usage.

To access the associated object you have to define accessor function in your Comment class:

class Comment extends \Entities\AbstractEntity{
    /** other definitions */

    function getEntity(){
        return $this->entity;
    }
}

And use it like

$comment = $em->find("Entities\Comment",1);
$entity = $comment->getEntity();

Doctrine2 will automatically populate $comment->entity proxy with actual Entity object.

See "Workin with Objects" chapter of Doctrine documentation and "Can you explain me what is a Proxy in Doctrine 2?" on details of proxies.



来源:https://stackoverflow.com/questions/7118617/doctrine-2-how-do-get-data-from-the-inverse-side-many-to-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!