我想找到按此顺序写的最新评论。
我的
控制器:
$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".
您正在尝试从另一个实体调用存储库函数
尝试改变这一行:
$order->getComments()->findLatest( $order->getId()
和:
$this->getDoctrine()->getRepository(Comments::class)->findLatest($order->getId);
更好的解决方案是您使用 $orders->getComments() 数组以避免在循环内从数据库请求数据
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句