2011-08-19 23 views
0

我有兩個實體,條目和註釋。doctrine 2,hwo從反面獲取數據(多對一)

評論:

/** 
* @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(); 
    } 

我能得到的通過屬於每個條目中的所有意見收集 -

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

,但我怎麼能得到來自評論方的條目信息。例如,我想從一個specfic評論

回答

0

每次創建@OneToMany關係時得到的條目ID,你的「一」關係的 - 側,並且單個代理對象創建類的代理對象的Collection關於「多」的課。代理類由Doctrine2根據您的映射信息自動生成。

要允許Doctrine2填充代理對象與數據庫中的實際數據,聲明protectedprivate是很重要的。我不確定這一點,但似乎像Doctrine追蹤代理實體類內的代理對象的請求,並確保代理在第一次使用之前被填充。

要訪問你在你Comment類中定義存取函數相關的對象:

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對象。

請參閱"Workin with Objects" chapter of Doctrine documentation"Can you explain me what is a Proxy in Doctrine 2?"關於代理的詳細信息。

+0

所以在我的情況下,它會是:function getEntity(){ return $ this-> entry; ? –

+0

在許多方面並不工作......應該是直接前進,因爲你想訪問相關數據... –

+0

我會發布一些代碼示例,我把它們放在我的家用計算機上。 – J0HN