如何为 getNumberOfTasks()设置id参数? id参数应具有数据库中id字段的值。我已经完成了 $ entities-> getId()但是我收到了一个错误,比如试图获取非对象的属性 DefaultController.php
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('TestBundle:Projects')->findAll();
$tasks = $em->getRepository('TestBundle:Tasks')->getNumberOfTasks($entities->getId);
/*dump($entities);
die();
*/
return $this->render('TestBundle:Default:index.html.twig', [
'projects' => $entities,
'tasks' => $tasks,
]
);
}
任务存储库
public function getNumberOfTasks($id)
{
$qb = $this->createQueryBuilder('p')
->where('p.projects = :id')
->setParameter('id', 1)
->getQuery();
return count($qb->getResult());
}
public function getNumberOfCompletedTasks($id)
{
$qb = $this->createQueryBuilder('p')
->where('p.projects = :id','p.completed = 1')
->setParameter('id', 1)
->getQuery();
return count($qb->getResult());
}
public function getPercentComplete($id)
{
$numberOfTasks = $this->getNumberOfTasks($id);
$numberOfCompletedTasks = $this->getNumberOfCompletedTasks($id);
if($numberOfTasks == 0)
return 100;
return ($numberOfCompletedTasks / $numberOfTasks) * 100;
}
答案 0 :(得分:1)
该行:
$entities = $em->getRepository('TestBundle:Projects')->findAll();
正在返回一个对象数组。
如果你想拥有id:
,你应该迭代实体foreach ($entities as $entity) {
$tasks[] = $em->getRepository('TestBundle:Tasks')->getNumberOfTasks($entity->getId);
}
然后在tasks