【发布时间】:2019-05-23 07:53:57
【问题描述】:
- 订单//订单
- 评论//每个订单的cmets
我想查找按此顺序编写的最新评论。
我的
控制器:
$orders = $this->getDoctrine()->getRepository(Orders::class)->findAll();
foreach($orders as $order) {
$temp = array(
$order->getId(),
$order->getComments()->findLatest( $order->getId() )
实体(评论):
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Orders", inversedBy="comments")
*/
private $orders;
实体(订单):
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection
{
return $this->comments;
}
评论库:
public function findLatest($value)
{
return $this->createQueryBuilder('c')
->andWhere('c.orders = :val')
->setParameter('val', $value)
->orderBy('c.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getResult()
;
}
但看起来它不能以这种方式工作:(
错误:
Attempted to call an undefined method
named "findLatest" of class "Doctrine\ORM\PersistentCollection".
【问题讨论】:
-
“不工作”是什么意思?当您调用
getComments时会发生什么-真正返回的内容是什么? -
@NicoHaase 感谢您的评论:尝试调用“Doctrine\ORM\PersistentCollection”类的名为“findLatest”的未定义方法。
-
那么,是什么让您认为可以在集合上调用
findLatest?你写过这样的方法吗?