本文介绍了原则2,如何从反面获取数据(多对一)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体,条目和注释。

I have two entities, entry and 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';
}

并输入:

<?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;
}

但是我如何从评论侧获取条目信息。例如,我想从特定注释中获取条目ID

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

推荐答案

每次创建 @OneToMany 关系,您可以在关系的一个侧上创建类的 Collection 代理对象,并在关系的许多方面。代理类由Doctrine2根据您的映射信息自动生成。

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.

要允许Doctrine2用数据库中的真实数据填充代理对象,重要的是声明受保护的 private 。我不确定这一点,但似乎Doctrine会跟踪对实体类中的代理对象的任何请求,并确保在首次使用之前填充代理。

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.

访问您必须在 Comment 类中定义访问器函数的关联对象:

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;
    }
}

并像

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

Doctrine2将自动填充 $ comment-> entity 具有实际 Entity 对象的代理。

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

请参见和您能解释一下什么是教义2中的代理吗? 有关代理的详细信息。

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

这篇关于原则2,如何从反面获取数据(多对一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 12:47