我'在互联网上搜索,但我没有找到这个问题的明确答案。 我有以下实体:
/**
* @ORM\Entity
* @ORM\Table(name="category")
*/
class Category
{
/*
* ...
*/
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
protected $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Add product
*
* @param \AppBundle\Entity\Product $product
*
* @return Category
*/
public function addProduct(\AppBundle\Entity\Product $product)
{
$this->products[] = $product;
return $this;
}
/**
* Remove product
*
* @param \AppBundle\Entity\Product $product
*/
public function removeProduct(\AppBundle\Entity\Product $product)
{
$this->products->removeElement($product);
}
/**
* Get products
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
}
/**
* @ORM\Entity
* @ORM\Table(name="product")
*/
class Product
{
/*
* ...
*/
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
/**
* @ORM\Column(type="boolean")
*/
protected $active;
/**
* Set category
*
* @param \AppBundle\Entity\Category $category
*
* @return Product
*/
public function setCategory(\AppBundle\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return \AppBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
/**
* Set active
*
* @param boolean $active
*
* @return Product
*/
public function setActive($active)
{
$this->active= $active;
return $this;
}
/**
* Get active
*
* @return boolean
*/
public function getActive()
{
return $this->active;
}
}
我只想这样做:
$em = $this->getDoctrine()->getManager();
$categories = $em->getRepository('AppBundle\Entity\Category')->findAll();
$json = $serializer->serialize($categories, 'json');
在json结果中,我想确保所有类别的关联产品按状态= true 进行过滤。换句话说我希望将结果作为一个类别对象数组放在前端,每个类别都有一系列仅活动的产品。
我知道它可以在Django中轻松完成。
可以在学说中完成吗?如果是,如何。 请海峡回答。
$qb = $em->createQueryBuilder()
->select('c')
->from('AppBundle:Category','c')
->leftJoin('c.products','p','WITH','p.status = :status')
->addSelect('p')
->setParameter('status', true);