symfony doctrine SQL QueryBuilder in entity to template

问题描述 投票:0回答:0

我的 referralentity.php 中有一个函数

public function getTotalReferrals($user)
{
  $qb = $this->createQueryBuilder('r');
    $qb->select('COUNT(r.id) AS totalReferral, r.referrer_id')
        ->where('r.status = :status')
        ->setParameter('status', 'Completed')
        ->groupBy('r.referrer_id')
        ->orderBy('totalReferral', 'DESC');
    return $qb->getQuery()->getResult();

}

在我的模板文件中,我尝试称它有效,但它只显示推荐 ID。

      {% for getTotalReferrals in getTotalReferrals %}
  <tr></tr>
  
  <td> Referrer ID: {{ getTotalReferrals.id }}</td>
<td>  Total Referrals: {{ getTotalReferrals.totalReferral }}</td>
    
{% endfor %}

我得到以下错误

Neither the property "totalReferral" nor one of the methods "totalReferral()", "gettotalReferral()"/"istotalReferral()"/"hastotalReferral()" or "__call()" exist and have public access in class "App\Entity\ReferralEntity".

是不是因为我也有

/**
 * Get id.
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

在实体中,因此它从该函数中提取 ID 并忽略我的 getTotalReferrals 函数?

我试图提取的原始查询是

LEFT JOIN SELECT COUNT(r.`id`) AS totalReferral, r.`referrer_id` FROM `referral` r WHERE r.`status` = 'Completed' GROUP BY r.`referrer_id` ORDER BY `totalReferral` DESC
symfony doctrine
© www.soinside.com 2019 - 2024. All rights reserved.