Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Cakephp表查询:总和返回0_Mysql_Cakephp - Fatal编程技术网

Mysql Cakephp表查询:总和返回0

Mysql Cakephp表查询:总和返回0,mysql,cakephp,Mysql,Cakephp,使用find对表事件中的字段num_days求和时出现问题 $events = TableRegistry::get('Events'); $query = $events->find('all') ->select(['used' => 'sum(num_days)']) ->first(); 为什么$query->used始终为0 print\r($events->find('all')->select

使用find对表事件中的字段num_days求和时出现问题

$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\u 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正是查询的正确答案,打印时会显示它,但$query->used始终返回0。

尝试
$query->order(['used'=>'DESC'])
$query->used之前


此外,我们还可以将
used
添加为
protected$\u virtual=['used']
内部
Event.php
实体文件

有什么问题?@Mahesh Singh Chouhan问题是$query->used没有返回6.try
$query->order(['used'=>'DESC'])之前
$query->used
你好。。。它起作用了!!谢谢,现在我面临一个新问题。此代码应放在Event.php(事件的实体)中。但是当我调用.ctp(view)文件中使用的$event->时,我认为代码处于循环中。我无法将代码放在控制器中,因为我需要一个where子句,该子句将被添加,并将使用每个实体的a字段值。我相信您可以在
事件中使用
virtualField
。php
as
used
来解决此问题,也让我添加上述解决方案作为答案,以便帮助其他面临相同问题的人