请我根据其所属的部门获取产品实体,但部门实体与产品实体没有直接关系。产品是这种方式的部门伟大的孩子 部 - >类别 - > GROUP-> PRODUCT。
DepartmentRepository
public function findDepartmentProduct($id)
{
return $this
->createQueryBuilder(['p', 'c', 'gc', 'ggc'])
->select(['p', 'c', 'gc', 'ggc'])
->from('Parent', 'p')
->leftJoin('p.children', 'c')
->leftJoin('c.grandchildren', 'gc')
->leftJoin('gc.greatgrandchildren', 'ggc')
->where('p.id = :id')
->andWhere('ggc.expire = :no')
->setParameter('no',0)
->setParameter('id', $id)
->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 = $Department->findDepartmentProduct($id);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1)/*page number*/,
8/*limit per page*/
);
return $this->render('default/department.html.twig', array(
'pagination' => $pagination,
'department'=>$department,
'customersChoiceProducts'=>$customersChoiceProducts,
));
}
视图:
<div class="link">
<ul>
{% for product in pagination %}
<li >
<a href="/brows/{{ product.id }}">
<span> <img src="{{ vich_uploader_asset(product, 'frontImageFile') }}" alt="" style="height:290px"></span>
<h2 class="title">
<span class="brand">{{ product.brand.name |title}}</span>
<span class="name">{{ product.name|title }}</span>
</h2>
<span class="price-box">
<span>
<span data-currency-iso="NGN">₦</span>
<span data-price="{{ product.price }}">{{ product.price }}</span>
</span>
</span>
</a>
</li>
{% endfor %}
</ul>
</div>
然后我运行代码我得到错误
错误:方法Doctrine \ ORM \ Query \ Expr \ From :: __ toString()不得抛出异常 500内部服务器错误 - FatalErrorException
答案 0 :(得分:1)
此:
->from('Parent', 'p')
应该是:
->from('YourNamespaceYourBundle:Parent', 'p')
或者:
->from('YourNamespace\YourBundle\Parent', 'p')
当然,为您的应用提供正确的命名空间或包名称。