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.