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.