在我的控制器中,我正在调用以下MySQL存储过程:
$details['haha'] = $this->Events->query("call sp_stats_by_event2(1,1)");
我得到以下输出:
然而,在HeidiSQL中,我得到了这个:
控制器返回两条记录。适当数量的记录应该是HeidiSQL返回的记录。
以下是存储过程的SQL:
CREATE DEFINER=`root`@`%` PROCEDURE `sp_stats_by_event2`(IN `event_id` INT, IN `user_id` INT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
select `events`.`id` AS `event_id`,`laps`.`user_id` AS `user_id`,
count(`laps`.`id`) AS `lap_count`,
sec_to_time(avg(time_to_sec(`laps`.`lap_time`))) AS `avg_lap_time`,
min(`laps`.`lap_time`) AS `min_lap_time`,
max(`laps`.`lap_time`) AS `max_lap_time`,
sec_to_time(sum(time_to_sec(`laps`.`lap_time`))) AS `total_event_time`
from (`events` join `laps` on((`events`.`id` = `laps`.`event_id`)))
where events.id = event_id and laps.user_id=user_id
group by `events`.`id`,`laps`.`user_id`;
END
在存储过程的代码中,我没有字段名称,event_type_id,event_sub_type_id,lap_distance和lap_target。 CakePHP必须取一些东西吗?可能解释了为什么我得到两个记录......
更新#1
行动的完整代码
public function details($event_id = null, $user_id = null) {
$details = $this->Events->find ('all')->where ( [
'Events.id' => $event_id
] )->contain ( [
'EventTypes',
'EventSubTypes',
'VwStatsByEvents',
'Laps' => [
'queryBuilder' => function ($q) {
return $q->order ( [
'Laps.lap_order'
] );
},
'Users' => [
'queryBuilder' => function ($q) use($user_id) {
return $q->where ( [
'Users.id' => $user_id
] );
}
]
]
] )->first();
$details['haha'] = $this->Events->query("call sp_stats_by_event2(1,1)");
$this->set ( compact ( 'details' ) );
$this->set ( '_serialize', [
'details'
] );
}
更新#2
我认为我通过事件查询的事实,我从事件模型中获取字段,这解释了它们的存在。但是,我的SP记录都没有出现。
更新#3
这是我能够从返回的SP调用中获取记录的唯一方法:
$conn = ConnectionManager::get('default');
$r = $conn->execute("call sp_stats_by_event2(1,1)")->fetchAll('assoc');