簡體   English   中英

DQL准則2限制協會

[英]Doctrine 2 Restricting Associations with DQL

在Doctrine 2.1中似乎有些疏漏,在其中返回關聯的子集並不容易。

http://www.doctrine-project.org/docs/orm/2.1/en/reference/limitations-and-known-issues.html#restricing-associations

文檔建議編寫一個存儲庫查找方法,這很有意義,因為這是我要做的第一件事。

但是,如果沒有在實體中引用EntityManager,我看不到如何檢索關聯的存儲庫,這似乎使將域與數據庫分離的觀點變得不對頭了?

有針對此問題的推薦策略嗎?

這是我對他們建議的解決方案的解釋。

class Category
{
    protected $id;
    protected $articles; // PesistentCollection
    protected $em; // The EntityManager from somewhere?

    public function getVisableArticles()
    {
        return $this->em->getRepository('Article')
                    ->getVisibleByCategory($this);
    }
}
  1. 在任何情況下,在實體中擁有entitymanager都不是一件好事(改為注入您的存儲庫)
  2. 類別不是文章的唯一根源,因為它無法確定所需的文章,因此您需要一個文章存儲庫。

我會怎么做:

class Category
{
    protected $id;
    protected $articles; // PesistentCollection

    public function getVisableArticles(IArticleRepository $articleRepository)
    {
        return $articleRepository->getVisibleByCategory($this);
    }
}

interface IArticleRepository
{
    function getVisibleByCategory(Category $category);
}

您的學說的存儲庫將實現IArticleRepository,並且該類對您的數據存儲/學說一無所知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM