请根据产品实体所属的部门获取产品实体,但该部门实体与产品实体没有直接关系。在这种方式下,产品是部门的伟大伟人->类别->集团->产品。
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
发布于 2016-02-12 03:11:19
这是:
->from('Parent', 'p')应当是:
->from('YourNamespaceYourBundle:Parent', 'p')或者:
->from('YourNamespace\YourBundle\Parent', 'p')当然,您的应用程序中有正确的名称空间或包名。
https://stackoverflow.com/questions/35353459
复制相似问题