Symfony2onetoworkflow problem!

Source: Internet
Author: User
There are now two tables, category and article. Category: {code ...} article: {code ...} my problem is that the $ category-& amp; gt; getArticles () method can get the articles under the specified category. However, if I only want to get status & quot; published & quot; how... there are now two tables, category and 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;    // ...}

My problem is that the $ category-> getArticles () method can get the articles under the specified category. But what if I only want to get status = "published? I have read the document and onetoworkflow cannot set conditions.

In addition, in twig, category. articles | length is used to display the number of articles under a specified category, and only all articles can be returned. How can this scenario be written ?.

In addition, I now use JOIN in CategoryRepository to add a condition query, but the result displayed in twig still ignores the status condition:

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

Reply content:

There are now two tables, category and 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;    // ...}

My problem is that the $ category-> getArticles () method can get the articles under the specified category. But what if I only want to get status = "published? I have read the document and onetoworkflow cannot set conditions.

In addition, in twig, category. articles | length is used to display the number of articles under a specified category, and only all articles can be returned. How can this scenario be written ?.

In addition, I now use JOIN in CategoryRepository to add a condition query, but the result displayed in twig still ignores the status condition:

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;

}

Solve the problem from the perspective of articles.

In controller

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

Then you can use articles | length in the view.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.