Php 如何在CDbCriteria中使用此SQL
我现在有这个SQLPhp 如何在CDbCriteria中使用此SQL,php,sql,yii,Php,Sql,Yii,我现在有这个SQL SELECT MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) Question1, MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) Question2, MAX(CASE WHEN uv.user_type_variables_id =
SELECT
MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) Question1,
MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) Question2,
MAX(CASE WHEN uv.user_type_variables_id = 3 THEN uv.value ELSE NULL END) Question3,
MAX(CASE WHEN uv.user_type_variables_id = 4 THEN uv.value ELSE NULL END) Question4,
MAX(CASE WHEN uv.user_type_variables_id = 5 THEN uv.value ELSE NULL END) Question5,
MAX(CASE WHEN uv.user_type_variables_id = 6 THEN uv.value ELSE NULL END) 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_user_variables AS uv on u.id = uv.user_id
GROUP BY ea.id
我需要将其纳入我现有的CDBC标准+扩展关系。我需要将每个问题1到6作为一个变量,我可以将其放入cgridview中,但我正在努力使其工作。如果我使用CSQLdataprovider,它可以正常工作,但我需要现有的标准
我目前的标准
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('event_id', $this->event_id, true);
$criteria->compare('status_id', $this->status_id, true);
$criteria->compare('checkin_status_id', $this->checkin_status_id, true);
// $criteria->compare('guest_invites', $this->guest_invites, true);
$criteria->compare('guest_of_user_id', $this->guest_of_user_id, true);
$criteria->compare('user_id', $this->user_id, true);
$criteria->compare('assign_group', $this->assign_group, true);
// $criteria->with = 'eventAttendeesGroup';
$criteria->with = 'user';
$criteria->compare('user.forename', $this->user_forename, true);
$criteria->compare('user.surname', $this->user_surname, true);
$criteria->compare('user.company', $this->user_company, true);
$criteria->compare('user.telephone', $this->user_telephone, true);
$criteria->compare('user.dob', $this->user_dateofbirth, true);
//need to get rid of this
$criteria->compare('userVariables.value', $this->userType_value, true);
$criteria->order = 'user.surname ASC';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
//manages the pagination and how many users appear on the onsite reg page
'pagination'=>array('pageSize'=>50),
//'pagination'=>false,
));
}
有人有什么想法吗
我假设我必须为每个问题设置变量,并将它们添加到一个关系中,比如usertype.variable和$this->variable,但完全不知道
编辑
我可以
function newsearch(){
$rawData=Yii::app()->db->createCommand('SELECT
MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) Question1,
MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) Question2,
MAX(CASE WHEN uv.user_type_variables_id = 3 THEN uv.value ELSE NULL END) Question3,
MAX(CASE WHEN uv.user_type_variables_id = 4 THEN uv.value ELSE NULL END) Question4,
MAX(CASE WHEN uv.user_type_variables_id = 5 THEN uv.value ELSE NULL END) Question5,
MAX(CASE WHEN uv.user_type_variables_id = 6 THEN uv.value ELSE NULL END) 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_user_variables AS uv on u.id = uv.user_id
GROUP BY ea.id')->queryAll();
// or using: $rawData=User::model()->findAll();
return $dataProvider=new CArrayDataProvider($rawData, array(
'id'=>'Questions',
'sort'=>array(
'attributes'=>array(
'Question1', 'Question2', 'Question3',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
在网格中显示sql,但我仍然无法访问与此同时显示的模型数据。我尚未对其进行彻底测试。但是您需要将属性添加到model类中,并将queryselect语句添加到CDbCriteria()属性中,然后使用该属性以便立即执行查询 我假设这是tbl_event_Attenders模型,如果使用正确,我将自动填充记录
class tblEventAttendees extends CActiveRecord {
public $Question1;
public $Question2;
public $Question3;
public $Question4;
public $Question5;
public $Question6;
...
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->select = [
't.*',
'user.*',
'MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) Question1',
'MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) Question2',
'MAX(CASE WHEN uv.user_type_variables_id = 3 THEN uv.value ELSE NULL END) Question3',
'MAX(CASE WHEN uv.user_type_variables_id = 4 THEN uv.value ELSE NULL END) Question4',
'MAX(CASE WHEN uv.user_type_variables_id = 5 THEN uv.value ELSE NULL END) Question5',
'MAX(CASE WHEN uv.user_type_variables_id = 6 THEN uv.value ELSE NULL END) Question6',
// add what you need
];
$criteria->compare('id', $this->id, true);
$criteria->compare('event_id', $this->event_id, true);
$criteria->compare('status_id', $this->status_id, true);
$criteria->compare('checkin_status_id', $this->checkin_status_id, true);
// $criteria->compare('guest_invites', $this->guest_invites, true);
$criteria->compare('guest_of_user_id', $this->guest_of_user_id, true);
$criteria->compare('user_id', $this->user_id, true);
$criteria->compare('assign_group', $this->assign_group, true);
// $criteria->with = 'eventAttendeesGroup';
$criteria->compare('user.forename', $this->user_forename, true);
$criteria->compare('user.surname', $this->user_surname, true);
$criteria->compare('user.company', $this->user_company, true);
$criteria->compare('user.telephone', $this->user_telephone, true);
$criteria->compare('user.dob', $this->user_dateofbirth, true);
//need to get rid of this
$criteria->compare('userVariables.value', $this->userType_value, true);
$criteria->order = 'user.surname ASC';
$criteria->with = array('user','event','userVariables');
$criteria->together = true;
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
//manages the pagination and how many users appear on the onsite reg page
'pagination'=>array('pageSize'=>50),
//'pagination'=>false,
));
}
更新
经过一些测试,我们发现问题不在于查询,而在于别名的使用。Yii使用别名映射查询结果,并使用preg_match函数从查询中提取别名,请参见
所以改变这个
$criteria->select = [
't.*',
'user.*',
'MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) Question1',
'MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) Question2',
'MAX(CASE WHEN uv.user_type_variables_id = 3 THEN uv.value ELSE NULL END) Question3',
'MAX(CASE WHEN uv.user_type_variables_id = 4 THEN uv.value ELSE NULL END) Question4',
'MAX(CASE WHEN uv.user_type_variables_id = 5 THEN uv.value ELSE NULL END) Question5',
'MAX(CASE WHEN uv.user_type_variables_id = 6 THEN uv.value ELSE NULL END) Question6',
// add what you need
];
这就解决了问题
$criteria->select = [
't.*',
'user.*',
'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',
// add what you need
];
我没有彻底测试过它。但是您需要将属性添加到model类中,并将queryselect语句添加到CDbCriteria()属性中,然后使用该属性以便立即执行查询 我假设这是tbl_event_Attenders模型,如果使用正确,我将自动填充记录
class tblEventAttendees extends CActiveRecord {
public $Question1;
public $Question2;
public $Question3;
public $Question4;
public $Question5;
public $Question6;
...
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->select = [
't.*',
'user.*',
'MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) Question1',
'MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) Question2',
'MAX(CASE WHEN uv.user_type_variables_id = 3 THEN uv.value ELSE NULL END) Question3',
'MAX(CASE WHEN uv.user_type_variables_id = 4 THEN uv.value ELSE NULL END) Question4',
'MAX(CASE WHEN uv.user_type_variables_id = 5 THEN uv.value ELSE NULL END) Question5',
'MAX(CASE WHEN uv.user_type_variables_id = 6 THEN uv.value ELSE NULL END) Question6',
// add what you need
];
$criteria->compare('id', $this->id, true);
$criteria->compare('event_id', $this->event_id, true);
$criteria->compare('status_id', $this->status_id, true);
$criteria->compare('checkin_status_id', $this->checkin_status_id, true);
// $criteria->compare('guest_invites', $this->guest_invites, true);
$criteria->compare('guest_of_user_id', $this->guest_of_user_id, true);
$criteria->compare('user_id', $this->user_id, true);
$criteria->compare('assign_group', $this->assign_group, true);
// $criteria->with = 'eventAttendeesGroup';
$criteria->compare('user.forename', $this->user_forename, true);
$criteria->compare('user.surname', $this->user_surname, true);
$criteria->compare('user.company', $this->user_company, true);
$criteria->compare('user.telephone', $this->user_telephone, true);
$criteria->compare('user.dob', $this->user_dateofbirth, true);
//need to get rid of this
$criteria->compare('userVariables.value', $this->userType_value, true);
$criteria->order = 'user.surname ASC';
$criteria->with = array('user','event','userVariables');
$criteria->together = true;
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
//manages the pagination and how many users appear on the onsite reg page
'pagination'=>array('pageSize'=>50),
//'pagination'=>false,
));
}
更新
经过一些测试,我们发现问题不在于查询,而在于别名的使用。Yii使用别名映射查询结果,并使用preg_match函数从查询中提取别名,请参见
所以改变这个
$criteria->select = [
't.*',
'user.*',
'MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) Question1',
'MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) Question2',
'MAX(CASE WHEN uv.user_type_variables_id = 3 THEN uv.value ELSE NULL END) Question3',
'MAX(CASE WHEN uv.user_type_variables_id = 4 THEN uv.value ELSE NULL END) Question4',
'MAX(CASE WHEN uv.user_type_variables_id = 5 THEN uv.value ELSE NULL END) Question5',
'MAX(CASE WHEN uv.user_type_variables_id = 6 THEN uv.value ELSE NULL END) Question6',
// add what you need
];
这就解决了问题
$criteria->select = [
't.*',
'user.*',
'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',
// add what you need
];
我建议您创建一个模型,并在Yii模型中使用它。通过这种方法,您可以像任何其他基于表格的模型一样使用所有Yii模型功能(除了插入、更新、删除操作之外),使现有功能与原始查询和条件保持一致:
CREATE OR REPLACE VIEW myviewname AS
... your SQL ...
在MYSQL数据库视图
创建之后,用于数据库视图。在此模型中,使用现有的自定义search()
。最后,根据需要在您的程序中使用它,例如在CGridView、报告等中。我建议您创建一个模型,并在Yii模型中使用它。通过这种方法,您可以像任何其他基于表格的模型一样使用所有Yii模型功能(除了插入、更新、删除操作之外),使现有功能与原始查询和条件保持一致:
CREATE OR REPLACE VIEW myviewname AS
... your SQL ...
在
MYSQL数据库视图
创建之后,用于数据库视图。在此模型中,使用现有的自定义search()
。最后,根据需要在程序中使用它,例如在CGridView、报表等中。是否确实要使用CDbCriteria,可以实现它,但使用CDbCommand和编写自定义查询可能更容易,并使用CarrayDataProvider,因为这是我继承的一个项目,有太多依赖于相同的功能。我曾考虑将两个数据提供程序作为一个数组添加并连接在一起,但我无法得到我想要的结果,因为在使用搜索/导出时,sql数据和模型中的数据根本没有链接,这是这一数据的最终游戏。好的,我添加了一个答案,请告诉我这是否有帮助。我会尝试一下。我已经将sql添加到cArraydataprovider中,并且sql工作正常。您是否知道将模型数据添加到数组中的任何资源,以便我知道下次如何正确执行此操作?因此您希望将整个模型添加到查询的结果集中?如果您不想,您可以向阵列添加任何内容这对CArrayprovider来说不是问题如果这就是您的意思如果您真的想使用CDbCriteria,可以实现它,但使用CDbCommand和编写自定义查询可能更容易,并使用CarrayDataProvider,因为这是我继承的一个项目,有太多依赖于相同的功能。我曾考虑将两个数据提供程序作为一个数组添加并连接在一起,但我无法得到我想要的结果,因为在使用搜索/导出时,sql数据和模型中的数据根本没有链接,这是这一数据的最终游戏。好的,我添加了一个答案,请告诉我这是否有帮助。我会尝试一下。我已经将sql添加到cArraydataprovider中,并且sql工作正常。您是否知道将模型数据添加到数组中的任何资源,以便我知道下次如何正确执行此操作?因此您希望将整个模型添加到查询的结果集中?如果你不想,你可以在数组中添加任何东西。这对CArrayprovider来说不会是问题,如果这就是你的意思。不幸的是,我无法让它工作。无论我让它看什么,它仍然在看EventAttenders表,就像uv一样。看起来像是usertypevariable问题。我已经添加了我需要的所有关系,但它仍然说它找不到该列。我已将某种工作CDATA提供程序添加到我的OP中,但我仍然无法访问模型数据。活动记录“EventAttendes”正在尝试选择一个无效列“MAX(当userTypeVariables.user\u type\u variables\u id=1然后userTypeVariables.value ELSE NULL END)Question1”。注意,该列必须存在于表中,或者是具有别名的表达式。但是它正在识别关系,所以我不明白为什么它找不到列嗯,好的,启用CWebLogRoute查看执行的查询,以便您可以对此进行故障排除,检查$criteria->together=true;如果未设置此属性,则设置正确。我将使用延迟加载来访问关系。是的,所有关系都很好。看起来该功能将不得不放弃。不知道为什么