我有一個簡單的查詢來選擇實體並使用限制語句。我正在使用Doctrine NativeQuery,因爲我在sql查詢中有FIELD()函數,並且我需要一組對象作爲結果。 該查詢起作用。使用'SQL_CALC_FOUND_ROWS'和Doctrine NativeQuery來獲取總行數
但是我還需要記錄的總數,所以我在第一個查詢中使用了SQL_CALC_FOUND_ROWS
。第一次得到結果後,我創建另一個ResultSetMapping,另一個$ nativeQuery,執行SELECT FOUND_ROWS() AS found_rows
,我總是獲得總數爲'1'。
$rsm = new ResultSetMapping();
$rsm->addEntityResult('\\MyCompany\\Administration\\Domain\\Model\\Applicant\\Applicant', 'a');
$rsm->addFieldResult('a', 'first_name', 'firstName');
$rsm->addFieldResult('a', 'last_name', 'lastName');
$query = $this->em->createNativeQuery('SELECT SQL_CALC_FOUND_ROWS * FROM recruitment_applicant ORDER BY FIELD(id,5,15,8,17,2,1,16,9,7,11,6,10,12,13,14,18)', $rsm);
$result = $query->getResult(); // this result is ok
$sqlCountRows = "SELECT FOUND_ROWS() AS found_rows";
$countRowsRsm = new ResultSetMapping();
$countRowsRsm->addScalarResult('found_rows', 'foundRows');
$countRowsQuery = $this->em->createNativeQuery($sqlCountRows,$countRowsRsm);
$rowsCount = $countRowsQuery->getResult();
$total = $rowsCount[0]['foundRows']; // result is '1' when it should be '16'
我用this的例子。