簡體   English   中英

在Symfony2中的實體獲取方法中選擇相關實體

[英]Select related entities in entity getter method in Symfony2

我如何在Symfony2中創建自定義查找器,如何從對象上下文(如getter方法)而不是存儲庫上下文中調用。 這是我在EntityRepository類中的查詢:

public function getUpVotes($trip_id)
{
    return count($this->getEntityManager()
        ->createQueryBuilder()
        ->select('t')
        ->from('VputiTripBundle:Trip', 't')
        ->join('t.ratings', 'r')
        ->where('r.trip = :tid')
        ->andWhere('r.up = :up')
        ->setParameters(['tid' => $trip_id, 'up' => 1])
        ->getQuery()
        ->getResult());
}

這樣做的目的是,我將能夠調用此$ model-> getUpVotes()而不是調用實體存儲庫並手動傳遞參數。

如果設置了關聯,則可以將get與過濾器一起使用。

public function getUpVotes()
{
    return $this->ratings->filter(
        function (RatingInterface $rating) {
            return 1 === $rating->getUp();
        }
    );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM