我希望以一对多的关系访问实体的伟大孩子。例如:部门实体(父母) - >类别实体(儿童) - >组实体(大孩子) - >产品实体(曾孙子)。我想根据他们的部门获取产品。
部门与类别的关系:
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="department")
*/
protected $category;
public function __construct()
{
$this->category = new ArrayCollection();
}
与部门和小组的类别关系:
/**
* @ORM\ManyToOne(targetEntity="Department", inversedBy="category")
* @ORM\JoinColumn(name="department_id", referencedColumnName="id")
*/
protected $department;
/**
* @ORM\OneToMany(targetEntity="Group", mappedBy="category")
*/
protected $group;
public function __construct()
{
$this->group = new ArrayCollection();
}
与类别和产品的群组关系:
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="group")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="group")
*/
protected $product;
public function construct()
{
$this->product = new ArrayCollection();
}
/**
* @ORM\ManyToMany(targetEntity="Brand", inversedBy="group")
* @ORM\JoinTable(name="brand_groups")
*/
private $brand;
public function __construct()
{
$this->brand = new ArrayCollection();
}
与群组的产品关系:
/**
* @ORM\ManyToOne(targetEntity="Group", inversedBy="product")
* @ORM\JoinColumn(name="groups_id", referencedColumnName="id")
*/
protected $group;
ProductRepository:
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
use AppBundle\Entity\Product;
/**
* ProductRepository
*/
class ProductRepository extends EntityRepository
{
public function findDepartmentProduct($department)
{
return $this
->createQueryBuilder('p')
->select(array('p', 'g','c','d'))
->from('Product', 'p')
->join('p.Group', 'g')
->join('g.Category', 'c')
->join('c.Department', 'd')
->where('d = :department')
->setParameter('department', $department)
->getQuery();
}
}
DefaultController:
/**
* @Route("/department/{id}", name = "department")
*/
public function departmentAction(Request $request,$id)
{
$Department = $this->getDoctrine()->getRepository('AppBundle:Department');
$department = $Department->find($id);
$Product = $this->getDoctrine()->getRepository('AppBundle:Product');
$customersChoiceProducts = $Product->mostView('20');
$query = $query = $Product->findDepartmentProduct($department);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$request->query->getInt('page', 1),
8/*limit per page*/
);
return $this->render('default/department.html.twig', array(
'pagination' => $pagination,
'department' => $department,
'customersChoiceProducts' => $customersChoiceProducts,
));
}
然后我收到此错误:
[语义错误]第0行,第63行靠近'g,产品p':错误:类AppBundle \ Entity \ Product没有名为groups的关联
请帮帮我。
答案 0 :(得分:0)
我犯的错误是加入实体类名而不是关联。我的新代码是:
public function findDepartmentProduct($department)
{
return $this
->createQueryBuilder('l')
->select('p')
->from('AppBundle:Product','p')
->join('p.group', 'g')
->join('g.category', 'c')
->join('c.department', 'd')
->where('d = :department')
->setParameter('department', $department)
->getQuery();
}
如果你运行它,你会收到以下错误:
无法统计选择两个FROM组件的查询,无法区分
您可以通过添加->getResult()
public function findDepartmentProduct($department)
{
return $this
->createQueryBuilder('l')
->select('p')
->from('AppBundle:Product','p')
->join('p.group', 'g')
->join('g.category', 'c')
->join('c.department', 'd')
->where('d = :department')
->setParameter('department', $department)
->getQuery()
->getResult();
}