Symfony2 oneToMany 問題!

來源:互聯網
上載者:User
現在有兩張表,category 和 article。

category:

/** * @Mapping\Entity * @Mapping\Table(name="category") */class Category{    /**     * @Mapping\Id     * @Mapping\Column(type="integer")     * @Mapping\GeneratedValue(strategy="AUTO")     */    protected $id;    /**     * @Mapping\OneToMany(targetEntity="Article", mappedBy="category")     */    protected $articles;    // ...}

article:

/** * @Mapping\Entity * @Mapping\Table(name="article") */class Article{    /**     * @Mapping\Id     * @Mapping\Column(type="integer")     * @Mapping\GeneratedValue(strategy="AUTO")     */    protected $id;    /**     * @Mapping\ManyToOne(targetEntity="Category", inversedBy="articles")     * @Mapping\JoinColumn(name="categoryid", referencedColumnName="id")     */    protected $category;        /**     * @Mapping\Column(type="string", columnDefinition="enum('published', 'draft')")     */    protected $status;    // ...}

我的問題是, $category->getArticles() 方法可以擷取指定分類下的 articles,但是,如果我只想擷取 status="published" 時怎麼辦呢?我看過文檔,oneToMany 中不能設定條件

同時,在 twig 中使用 category.articles|length 來顯示指定分類下的文章數,也只能返回全部的文章,這種情境怎麼寫呢?。

另外,我現在在 CategoryRepository 中使用 JOIN 加上條件查詢出來,但在 twig 中顯示出來的結果就還是忽略了 status 條件:

return $this->createQueryBuilder('c')     ->leftJoin('c.articles', 'a', 'WITH', 'a.status = :status')     ->groupBy('c.id')     ->setParameter('status', 'published')     ->getQuery()     ->getResult();

回複內容:

現在有兩張表,category 和 article。

category:

/** * @Mapping\Entity * @Mapping\Table(name="category") */class Category{    /**     * @Mapping\Id     * @Mapping\Column(type="integer")     * @Mapping\GeneratedValue(strategy="AUTO")     */    protected $id;    /**     * @Mapping\OneToMany(targetEntity="Article", mappedBy="category")     */    protected $articles;    // ...}

article:

/** * @Mapping\Entity * @Mapping\Table(name="article") */class Article{    /**     * @Mapping\Id     * @Mapping\Column(type="integer")     * @Mapping\GeneratedValue(strategy="AUTO")     */    protected $id;    /**     * @Mapping\ManyToOne(targetEntity="Category", inversedBy="articles")     * @Mapping\JoinColumn(name="categoryid", referencedColumnName="id")     */    protected $category;        /**     * @Mapping\Column(type="string", columnDefinition="enum('published', 'draft')")     */    protected $status;    // ...}

我的問題是, $category->getArticles() 方法可以擷取指定分類下的 articles,但是,如果我只想擷取 status="published" 時怎麼辦呢?我看過文檔,oneToMany 中不能設定條件

同時,在 twig 中使用 category.articles|length 來顯示指定分類下的文章數,也只能返回全部的文章,這種情境怎麼寫呢?。

另外,我現在在 CategoryRepository 中使用 JOIN 加上條件查詢出來,但在 twig 中顯示出來的結果就還是忽略了 status 條件:

return $this->createQueryBuilder('c')     ->leftJoin('c.articles', 'a', 'WITH', 'a.status = :status')     ->groupBy('c.id')     ->setParameter('status', 'published')     ->getQuery()     ->getResult();

class Category
{
...
public function getPublishedArticles()
{

$result = new ArrayCollection();foreach ($this->articles as $article) {    if ($article->getStatus() == 'published') {        $result[] = $article;    }}return $result;

}

從articles的角度解決問題。

在controller裡面

$articles = $this    ->getDoctrine()    ->getRepository('Article')    ->findBy([        'category' => $category,        'status'   => 'published',        ]);

然後view裡面就可以用articles|length了。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.