-3
我正在爲性能監視做一個網站。以條件獲取實體字段的最後一個值
我有2個表:
用戶表中的一個一對多的連接與性能
- 在鏈接
性能表多到一個與用戶表
我只想得到最後的重量這不是空的在我的表中表演並顯示它在樹枝
database screenshot 對於這個數據庫爲例,其結果必然是:80
我試圖與查詢,但我得到一個消息arror,所以我不知道該怎麼辦 預先感謝您的幫助!
我正在爲性能監視做一個網站。以條件獲取實體字段的最後一個值
我有2個表:
用戶表中的一個一對多的連接與性能
性能表多到一個與用戶表
我只想得到最後的重量這不是空的在我的表中表演並顯示它在樹枝
database screenshot 對於這個數據庫爲例,其結果必然是:80
我試圖與查詢,但我得到一個消息arror,所以我不知道該怎麼辦 預先感謝您的幫助!
我希望這可以幫助你。 我想你有一個性能實體的存儲庫類。如果你需要得到 :我會用這種代碼的Symfony3應用
<?php
namespace AppBundle\Repository; //replace AppBundle by the name of your bundle
use Doctrine\ORM\Tools\Pagination\Paginator;
/**
* PerformanceRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class PerformanceRepository extends \Doctrine\ORM\EntityRepository
{
public function getLastWeigth()
{
$qb = $this->getEntityManager()->createQueryBuilder()
->select('p')
->from($this->_entityName, 'p')
->expr()->isNotNull('p.weight')
->orderBy('p.date', 'desc')
->setMaxResults(1);
$query = $qb->getQuery();
$result = $query->getSingleResult();
return $result;
}
}
編輯:這裏是控制器使用的Symfony的3應用的例:
<?php
namespace AppBundle\Controller\Performance;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Repository\PerformanceRepository;
class PerformanceController extends Controller
{
public function lastWeightAction()
{
$repo = $this->getDoctrine()->getManager()->getRepository('AppBundle:Performance');
$lastPerformance = $repo->getLastWeigth();
//some other code
}
}
EDIT2通過用戶ID的最後重量:
class PerformanceRepository extends \Doctrine\ORM\EntityRepository
{
public function getLastWeigth($userId)
{
$qb = $this->getEntityManager()->createQueryBuilder()
->select('p')
->from($this->_entityName, 'p')
->join('p.user', 'u')
->where('u.id = :userId')
->expr()->isNotNull('p.weight')
->orderBy('p.date', 'desc')
->setMaxResults(1);
->setParameter(':userId', $userId);
$query = $qb->getQuery();
$result = $query->getSingleResult();
return $result;
}
}
謝謝您的回答,我們都ok的命令得到它在CONTROLER是:$ lastWeight = $用戶> getPerformance() - > getLastWeight(); ? 因爲它說:嘗試調用類「Doctrine \ ORM \ PersistentCollection」中名爲「getLastWeight」的未定義方法。 – user6533866
我編輯我的答案,告訴我如何在我的控制器中使用存儲庫方法 – Mz1907
好吧,現在我得到這個錯誤:調用成員函數orderBy()字符串 但日期字段是我不明白的日期時間類型 – user6533866