我目前正在尝试传递变量以像where子句一样操作,因此我可以将其传递给URL并显示页面上每个事件的内容。
为此,我需要传递event_id。
public function exportSearch($event_id){
$query = "SELECT
u.prefix AS 'Title',
u.forename AS 'Forename',
u.surname AS 'Surname',
u.telephone AS 'Mobile Number',
u.email AS 'Email Address',
ea.checkin_status_id AS 'checked in',
u.update_time AS 'Register Date',
eg.name AS 'Event Group Registered',
e.name AS 'Session Registered',
u.bio AS 'User Information',
u.dob AS 'Date of Birth',
u.company AS 'Company',
u.company_role AS 'Company Role',
CONCAT(u2.forename, ' ', u2.surname) AS 'Guest of',
MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) AS Question1,
MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) AS Question2,
MAX(CASE WHEN uv.user_type_variables_id = 3 THEN uv.value ELSE NULL END) AS Question3,
MAX(CASE WHEN uv.user_type_variables_id = 4 THEN uv.value ELSE NULL END) AS Question4,
MAX(CASE WHEN uv.user_type_variables_id = 5 THEN uv.value ELSE NULL END) AS Question5,
MAX(CASE WHEN uv.user_type_variables_id = 6 THEN uv.value ELSE NULL END) AS Question6
FROM tbl_event_attendees AS ea
LEFT JOIN tbl_user AS u ON ea.user_id = u.id
LEFT JOIN tbl_event AS e ON ea.event_id = e.id
LEFT JOIN tbl_event_groups AS eg on e.group_id = eg.id
LEFT JOIN tbl_user AS u2 ON ea.guest_of_user_id = u2.id
LEFT JOIN tbl_user_variables AS uv on u.id = uv.user_id
GROUP BY ea.id";
// $query->params([':event_id' => $event_id]);
$count=Yii::app()->db->createCommand($query)->queryScalar();
return $sqlDataprovider = new CSqlDataProvider($query, array(
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
// 'event_name'=>Event::model()->getAttributeLabel('name'), // csv of sortable column names
//'Question1' => EventAttendees::model()->getAttributeLabel('Question1'),
// 'Forename',
)
),
'pagination'=>array(
'pageSize'=>$count, //Show all records
),
));
}
我的想法是使用$ query-> params来执行此操作,然后将其扔到视图中但没有运气。
它需要显示类似WHERE ea.event_id = $ event_id的东西,但我似乎无法通过原始sql传递它。
答案 0 :(得分:0)
您需要在SQL中添加WHERE子句,如下所示:
WHERE ea.event_id = :event_id
。请记住,您的$query
变量只包含SQL,因此这是一个字符串,而不是类似于查询对象。
要将参数传递给queryScalar()
函数,您需要传递一个包含参数的数组。根据{{3}}:
$count = Yii::app()->db->createCommand($query)->queryScalar(array(
':event_id'=>$event_id
));
注意:强>
文档说:
执行SQL语句并返回第一列的值 在第一行数据。这是一种方便查询的方法 只需要一个标量值(例如,获得计数值) 记录)。
因此,您的第一列u.prefix AS 'Title'
是字符串。将其用作$count
不是一个好的解决方案。我建议编写一个用于获取行数的SQL,另一个用于获取将传递给CSqlDataProvider
的数据。
答案 1 :(得分:0)
sql应该是这个
"SELECT
u.prefix AS 'Title',
u.forename AS 'Forename',
u.surname AS 'Surname',
u.telephone AS 'Mobile Number',
u.email AS 'Email Address',
ea.checkin_status_id AS 'checked in',
u.update_time AS 'Register Date',
eg.name AS 'Event Group Registered',
e.name AS 'Session Registered',
u.bio AS 'User Information',
u.dob AS 'Date of Birth',
u.company AS 'Company',
u.company_role AS 'Company Role',
CONCAT(u2.forename, ' ', u2.surname) AS 'Guest of',
MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) AS Question1,
MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) AS Question2,
MAX(CASE WHEN uv.user_type_variables_id = 3 THEN uv.value ELSE NULL END) AS Question3,
MAX(CASE WHEN uv.user_type_variables_id = 4 THEN uv.value ELSE NULL END) AS Question4,
MAX(CASE WHEN uv.user_type_variables_id = 5 THEN uv.value ELSE NULL END) AS Question5,
MAX(CASE WHEN uv.user_type_variables_id = 6 THEN uv.value ELSE NULL END) AS Question6
FROM tbl_event_attendees AS ea
LEFT JOIN tbl_user AS u ON ea.user_id = u.id
LEFT JOIN tbl_event AS e ON ea.event_id = e.id
LEFT JOIN tbl_event_groups AS eg on e.group_id = eg.id
LEFT JOIN tbl_user AS u2 ON ea.guest_of_user_id = u2.id
LEFT JOIN tbl_user_variables AS uv on u.id = uv.user_id
WHERE ea.id = " . $event_id . "
GROUP BY ea.id"