2017-05-08 94 views
0

我使用查找來總結表事件的字段num_days時遇到問題。Cakephp表查詢:SUM返回0

$events = TableRegistry::get('Events'); 
$query = $events->find('all') 
       ->select(['used' => 'sum(num_days)']) 
       ->first(); 

爲什麼$query->used總是0?

print_r($events->find('all')->select(['used' => 'sum(num_days)'])->toArray())給人,

Array ([0] => App\Model\Entity\Event Object ([used] => 6 [[new]] => [[accessible]] => Array ([*] => 1) [[dirty]] => Array () [[original]] => Array () [[virtual]] => Array () [[errors]] => Array () [[invalid]] => Array () [[repository]] => Events)) 

6恰好是用於查詢的正確答案和print_r的顯示,但$查詢 - >使用總是返回0。

+0

是什麼問題? –

+0

@Mahesh Singh Chouhan問題在於$ query-> used沒有返回6. – user2526028

+0

嘗試'$ query-> order(['used'=>'DESC']);'在'$ query-> used'之前' –

回答

0

嘗試$query->order(['used' => 'DESC']);$query->used

此外,我們可以添加used作爲protected $_virtual = ['used'];裏面Event.php實體文件