【发布时间】:2016-05-23 21:38:09
【问题描述】:
我想以一对多的关系访问实体的曾孙。例如:部门实体(父)-> 类别实体(子)-> 集团实体(孙)-> 产品实体(曾孙)。我想根据他们的部门获取产品。
与类别的部门关系:
/**
* @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();
}
}
默认控制器:
/**
* @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,
));
}
然后我得到这个错误:
[Semantical Error] line 0, col 63 near 'g, product p': Error: Class AppBundle\Entity\Product has no association named groups
请帮帮我。
【问题讨论】:
-
你加入的是关联,而不是实体类名,所以你应该有
->join('p.group', 'g')而不是->join('p.Group', 'g') -
谢谢@Jason 正在使用 knp 分页器,现在我收到此错误无法计数选择两个 FROM 组件的查询,无法区分。 ->createQueryBuilder('p') ->select('p') ->from('AppBundle:Product','l') ->join('p.group', 'g') ->join('g .category', 'c') ->join('c.department', 'd') ->where('d = :department') ->setParameter('department', $department) ->getQuery();
-
试试
->createQueryBuilder('p')->join('p.group', 'g') ->join('g.category', 'c') ->join('c.department', 'd') ->where('d = :department') ->setParameter('department', $department) ->getQuery(); -
谢谢你,我通过添加 ->getResult() 修复了它。所以我没有将查询传递给 knp 分页器,而是传递了结果
标签: php mysql symfony doctrine-orm