Symfony2 onetomany Problem!

Source: Internet
Author: User
Now there are 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 question is, $category->getarticles () method can get articles under the specified category, but what if I only want to get Status= "published"? I've read the document, and I can't set conditions in Onetomany.

At the same time, the use of category.articles|length in twig to display the number of articles under the specified classification, can only return all the articles, how to write this scene?

In addition, I now use JOIN plus conditional query in Categoryrepository, but the status condition is ignored in the results shown in twig:

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

Reply content:

Now there are 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 question is, $category->getarticles () method can get articles under the specified category, but what if I only want to get Status= "published"? I've read the document, and I can't set conditions in Onetomany.

At the same time, the use of category.articles|length in twig to display the number of articles under the specified classification, can only return all the articles, how to write this scene?

In addition, I now use JOIN plus conditional query in Categoryrepository, but the status condition is ignored in the results shown in twig:

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.

Inside the controller.

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

Then you can use Articles|length in the view.

  • Related Article

    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.