Difference between #5 and #6 of
Use CGridview to sort and filter CSqlDataProvider - when used with UNRELATED tables (or complex queries)

Revision #6 has been created by Gerhard Liebenberg on Oct 28, 2014, 2:27:57 PM with the memo:

Added injection free sql
« previous (#5)

Changes

Title unchanged

Use CGridview to sort and filter CSqlDataProvider - when used with UNRELATED tables (or complex queries)

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

CSqlDataProvider unrelated

Content changed

[...]
// If gv_hospitals was received from CGridview, then
// filter records with a WHERE clause
if( ($this->gv_hospitals !== null) &&
($this->gv_hospitals !== ''))
{
// The LIKE operator in the WHERE clause acts the same
 
// as this line in Relational Query:
$criteria = new CDbCriteria; // $criteria->compare('hosp_name',$this->gv_hospitals, true); $sql="
 
SELECT
 
condition = $criteria->condition;
 
$params = $criteria->params;
 
unset($criteria);
 
 
 
$sql = Yii::app()->db->createCommand()
 
->select("
 
    
concat(tbl_hosp.hosp_id, tbl_dept.dept_id) AS compkey,         tbl_hosp.hosp_id    AS hosp_id,         tbl_hosp.hosp_name  AS hosp_name,         tbl_dept.dept_name   AS dept_name
 
FROM
 
")
 
->from("
tbl_hosp, tbl_dept
 

 
WHERE
 
tbl_hosp.hosp_name LIKE '%" . $this->gv_hospitals . "%'";
 
}
 
else
 
{
 
$sql="
 
SELECT
 
")
 
->where($condition, $params);
 
}
 
else
 
{
 
$sql = Yii::app()->db->createCommand()
 
->select("
 
    
concat(tbl_hosp.hosp_id, tbl_dept.dept_id) AS compkey,         tbl_hosp.hosp_id    AS hosp_id,         tbl_hosp.hosp_name  AS hosp_name,         tbl_dept.dept_name   AS dept_name
 
FROM
 
")
 
->from("
tbl_hosp, tbl_dept");
}

return new CSqlDataProvider($sql, array(

// Use the composite key to keep the (hidden) key values of your
[...]
0 0
3 followers
Viewed: 24 776 times
Version: 1.1
Category: How-tos
Written by: Gerhard Liebenberg
Last updated by: Gerhard Liebenberg
Created on: Oct 26, 2014
Last updated: 9 years ago
Update Article

Revisions

View all history