以下查询返回包含正确ID的数组,但对所有值都为null。
如果我删除聚合函数(AVG()
),它会返回值(当然不是平均值),如果我选择例如find('all')
它返回平均值,但不是我想要的列表格式(我可以使用它,但我想首先尝试使用'list'
。)
$progress = $this->Trial->find('list', array(
'fields' => array(
'Trial.session_id',
'AVG(Trial.first_reaction_time_since_probe_shown) AS average_reaction_time'
),
'group' => 'Trial.session_id',
'conditions' => array(
'Trial.first_valid_response = Trial.probe_on_top',
'TrainingSession.user_id IS NOT NULL'
),
'contain' => array(
'TrainingSession' => array(
'conditions' => array(
'TrainingSession.user_id' => $this->Auth->user('id')
)
)
),
'recursive' => 1,
));
当我通过PhpMyAdmin将其发送到数据库时,生成的SQL查询将返回我想要的结果。
SELECT
`Trial`.`session_id`,
AVG(`Trial`.`first_reaction_time_since_probe_shown`) AS average_reaction_time
FROM
`zwang`.`trials` AS `Trial`
LEFT JOIN
`zwang`.`training_sessions` AS `TrainingSession` ON (
`Trial`.`session_id` = `TrainingSession`.`id` AND
`TrainingSession`.`user_id` = 1
)
WHERE
`Trial`.`first_valid_response` = `Trial`.`probe_on_top`
GROUP BY
`Trial`.`session_id`
我已经检查了the source for find('list')。我认为这是由于在查询中使用函数时访问列表的“数组路径”搞砸了,但我还是无法修复它(或者认识到我滥用CakePHP逻辑)。
答案 0 :(得分:2)
我发布问题后,Stackoverflow开始将correct answers与我联系起来。
显然,如果没有'list'
virtualFields
,则无法完成
我没想到,因为它使用其他查找类型。
$this->Trial->virtualFields = array(
'average_reaction_time' => 'AVG(Trial.first_reaction_time_since_probe_shown)'
);
$progress = $this->Trial->find('list', array(
'fields' => array('Trial.session_id','average_reaction_time')
/* etc... */
));