yii : CDbCriteria print out wrong result - php

problem : CDbCriteria print out wrong result.
This is the model search function.
public function summary()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->select = 't.*';
if (Yii::app()->user->checkAccess('Reviewer')):
$criteria->addSearchCondition('t.reviewers',Yii::app()->user->id,false, 'AND');
endif;
if (Yii::app()->user->checkAccess('Implementer')):
$criteria->addSearchCondition('t.implementers',Yii::app()->user->id,false, 'OR');
endif;
if (Yii::app()->user->checkAccess('Creator')):
$criteria->compare('t.creator',Yii::app()->user->id, false, 'OR');
endif;
$criteria->compare('request_type_id',$this->request_type_id);
$criteria->compare('request_category_id',$this->request_category_id);
$criteria->compare('financial_type_id',$this->financial_type_id);
$criteria->compare('urgency_id',$this->urgency_id);
$criteria->compare('eta_date',$this->eta_date,true);
if (Yii::app()->user->checkAccess('Reviewer') || Yii::app()->user->checkAccess('Implementer')):
$criteria->join = "JOIN profiles p ON (p.user_id = t.creator)";
if($this->creator):
$criteria->addSearchCondition('p.firstname',$this->creator, true, 'AND');
endif;
endif;
if(isset($this->status)):
$criteria->compare('t.status',$this->status);
endif;
$data = new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
'pagination'=>false,
));
$_SESSION['all'] = $data;
$data = new CActiveDataProvider(get_class($this), array(
'pagination'=>array('pageSize'=> Yii::app()->user->getState('pageSize',
Yii::app()->params['defaultPageSize']),),
'criteria'=>$criteria,
));
$_SESSION['limited'] = $data;
return $data;
}
above code was print out the query when I trace with CWebLogRoute but there are no record.
Querying SQL: SELECT t.* FROM request t JOIN profiles p ON
(p.user_id = t.creator) WHERE t.reviewers LIKE :ycp1 LIMIT 10
When I change those query to following one, it was print out the records.
SELECT t.* FROM request t JOIN profiles p ON ( p.user_id =
t.creator ) WHERE t.reviewers LIKE '%3%' LIMIT 0 , 30
How can I solve the problem?

Try change
$criteria->addSearchCondition('t.reviewers',Yii::app()->user->id,false, 'AND');
to
$criteria->addSearchCondition('t.reviewers',Yii::app()->user->id, true, 'AND');
Also check format of reviewers and how it write down in the records: it's maybe record as ' 3', '3 ', or ' 3 '.
It's have to update records by update request set reviewers = trim(reviewers)

Related

CActiveDataProvider doesnt give selected columns

$criteria->select = 't.*,count(business_unit.id) as TotalBusinessUnits,count(users.id) as TotalUsers ,count(positions.id) as TotalPositions, count(skills.id) as TotalSkills , users.first_name , users.last_name, count(question_bank.id) as TotalQuestions';
$criteria->condition = "`t`.status = '1'";
$criteria->group = 't.id';
$criteria->order = 'created DESC';
$criteria->join = " inner join users on users.company_id = `t`.id";
$criteria->join .= " left join business_unit on business_unit.company_id = `t`.id";
$criteria->join .= " left join billingdetails on billingdetails.company_id = `t`.id";
$criteria->join .= " left join positions on positions.company_id = `t`.id";
$criteria->join .= " left join skills on skills.company_id = `t`.id";
$criteria->join .= " left join question_bank on question_bank.company_id = `t`.id";
It works fine and get all columns which I select.
But when I pass it in CActiveDataProvider it gets only this attributes.
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination' => $pages
));
How I select all columns?
In Yii if you are using as for sql, it needs a public variable to store the value in.
add all your custom column names as public variables on the model and all should be dandy.
so if you have $criteria->select = 't.*,count(business_unit.id) as TotalBusinessUnits, this will create the corresponding sql . but Yii does not recognise TotalBusinessUnits as a valid column on the table.
So if you add public $TotalBusinessUnits; in your model Yii has a place to store this value.
Also you should then add TotalBusinessUnits to your safe attributes rule so that Yii knows it safe to fetch.
public $TotalBusinessUnits;
public function rules(){
return array(
// your rules
array('TotalBusinessUnits','safe'),
);
}
public function search(){
$criteria->select = 't.*,count(business_unit.id) as TotalBusinessUnits';
// your criteria
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}

Subquery With Yii Active Record

Is it possible to write sub queries with Yii active record?
I need to add a subquery in the FROM clause. Below is the SQL with the subquery but I want to specifically write it using Active Record.
In SQL, Policy has a many to many relationship with policy_vehicles.
SQL Query:
select * from (SELECT Name , GROUP_CONCAT(PV.VehicleId SEPARATOR ',') as VehicleIds
FROM `policies` `t`
LEFT JOIN policy_vehicles PV USING(PolicyId)
WHERE (Type IN ('C', 'E')) AND (Status='Active')
GROUP BY t.PolicyId
) k where VehicleIds like '%1%' AND VehicleIds like '%2%'
This is how I want to use it in the Controller class:
$model = Policy::model();
$model->Status = array('Active');
$model->VehicleIds = array(1,2);
$model->Type = array('C', 'E');
$datas = $model->search()->getData();
print_r($datas);
Search function in the model:
public function search() {
$joins = array();
$criteria = new CDbCriteria;
$criteria->compare('Type', $this->Type);
$criteria->compare('Status', $this->Status);
array_push($joins, 'LEFT JOIN policy_vehicles PV USING(PolicyId)');
$criteria->join = implode(' ', $joins);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}

PHP Yii: how to row numbering CGridView if using sqlite database

I want to show a filter-able and sort-able column of row number in CGridView, but what i have got is only an empty sort-able (clickable header) and filter-able (it has filtering text box up there) column.
In model (ActiveRecord) class i have added a new public attribute:
public $number;
in the rules() function i have added:
public function rules()
{
return array(
...
...
array('
...
...
number,
',
'safe',
'on'=>'search'
),
);
}
on the search() function, i have also add:
public function search()
{
$criteria=new CDbCriteria;
...
...
$number_select = '(
select count(*)
from tbl_mytable as k
where k.id <= t.id
)';
$criteria->select = array(
$number_select . ' as number',
't.*',
);
...
...
$criteria->compare($number_select, $this->number,true);
$sort = new CSort;
$sort->attributes = array(
'*',
...
...
'number'=>array(),
);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>$sort,
));
}
I've successfully using this kind of method to add other columns.
With the lack of skill and knowledge of sqlite query, i am suspecting the cause of this problem is the query in the $criteria->select.
I hope someone would help me for solving this.
Try adding the property to CActiveRecord.attributeNames:
public function attributeNames()
{
return array_merge(parent::attributeNames(), array('number'));
}
after editing my question, i edited my code a fad that miraculously turn out to be the answer.
Just remove the table alias from the select statement
Original (not working) code:
$number_select = '(
select count(*)
from tbl_mytable as k
where k.id <= t.id
)';
The new (working) code after removing table alias:
$number_select = '(
select count(*)
from tbl_mytable
where tbl_mytable.id <= t.id
)';
While my question is about yii with sqlite database, but since this is quite standard query, i think this solution will also works for yii with mysql database

Yii CDBCriteria Complex Join Query

I have 3 tables:
bank
bank_details
bank_bank_details
Relation between them:
bank model:
public function relations() {
return array(
'bankBankDetails' => array(self::HAS_MANY, 'BankBankDetails', 'bank_id'),
);
}
bank_details model:
public function relations() {
return array(
'bankBankDetails' => array(self::HAS_MANY, 'BankBankDetails', 'bank_details_id'),
);
}
bank_bank_details model:
public function relations()
{
return array(
'bank' => array(self::BELONGS_TO, 'Bank', 'bank_id'),
'bankDetails' => array(self::BELONGS_TO, 'BankDetails', 'bank_details_id'),
);
}
I want to fetch bank details like bank_name, ifsc etc info in admin view of bank_details model.
The normal SQL query I have generated which works perfectly fine:
SELECT b.name
FROM bank b
LEFT JOIN bank_bank_details bbd ON bbd.bank_id = b.bank_id
LEFT JOIN bank_details bd ON bd.bank_details_id = bbd.bank_details_id
WHERE bd.bank_details_id = bbd.bank_details_id
LIMIT 0 , 30
Now I just wants to integrate this with Yii CDBCriteria but its not working for me. Please check the code below:
public function search() {
$criteria = new CDbCriteria;
// select b.name
// from bank b
// left join bank_bank_details bbd
// on bbd.bank_id = b.bank_id
// left join bank_details bd on bd.bank_details_id = bbd.bank_details_id
// WHERE bd.bank_details_id = bbd . bank_details_id;
$criteria->compare('bank_details_id', $this->bank_details_id);
$criteria->compare('first_holder_name', $this->first_holder_name, true);
$criteria->compare('nominee1', $this->nominee1, true);
$criteria->select = 'b.name';
$criteria->join = 'LEFT JOIN bank_bank_details bbd ON bbd.bank_id = b.bank_id ';
$criteria->join .= 'LEFT JOIN bank_details bd ON bd.bank_details_id = bbd.bank_details_id';
$criteria->condition = 'bd.bank_details_id = bbd.bank_details_id';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 10,
),
));
}
Error:
Error 500 Found
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'b.bank_id' in 'on clause'
Any help would be appreciated.
You need to set alias to your table like this:
$criteria->alias='b'; or use default alias "t" instead of "b"
read more at
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#alias-detail
Try:
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('bank_details_id', $this->bank_details_id);
$criteria->compare('first_holder_name', $this->first_holder_name, true);
$criteria->compare('nominee1', $this->nominee1, true);
$criteria->select = 'bank.name';
$criteria->with = array(
'bankBankDetails' => array('joinType'=>'LEFT JOIN'),
'bankBankDetails.bank' => array('joinType'=>'LEFT JOIN'),
);
$criteria->addCondition('t.bank_details_id = bankBankDetails.bank_details_id');
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 10,
),
));
}
Main table (which is in FROM) has alias t and after compare you already having some condition, than you need to add to existing, not rewrite
UPDATE
If you do query from model bank_details then the main table (in FROM) must be bank_details. Query will be:
SELECT b.name
FROM bank_details bd
LEFT JOIN bank_bank_details bbd ON bd.bank_details_id = bbd.bank_details_id
LEFT JOIN bank b bbd ON bbd.bank_id = b.bank_id
WHERE bd.bank_details_id = bbd.bank_details_id
LIMIT 0 , 30
here
$criteria->select = 'b.name';
You are selecting only bank name and no id is selected
Not sure with the yii format something like
$criteria->select = 'b.name,b.bank_id';
or
$criteria->select = array('b.name,b.bank_id');
As #Evgeniy mentioned, you will need to set the alias using $criteria->alias = 'b';
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#alias-detail
You might also want to look into using CDbCriteria::with you can use this to select related models.
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#with-detail
If you use the above, make sure you also use CDbCriteria::together correctly. To select all related models at once, set it to true;
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#together-detail

How do I specify the tablename in a filter in Yii?

There's two tables, attendee, and group. both have a gender field. Each attendee is assigned to 1 group. I've got my CGridView successfully showing the number of attendees, but when I added code to filter on gender, I get the following error. To calculate the number of attendees in a group, I'm manually performing a join; but the join makes the field 'gender' ambiguous. Is there any way to specify in the filter that it should use the 't' or base table?
When I load the view it operates correctly; when I click to sort on gender, it gives me this message:
Error Message
> Error 500: <h1>CDbException</h1>
> <p>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'gender'
> in where clause is ambiguous. The SQL statement executed was: SELECT
> COUNT(*) FROM (SELECT t.*, IFNULL( count(tbl_attendee.id), 0) as
> attendeeCount FROM `tbl_group` `t` LEFT JOIN tbl_attendee ON
> tbl_attendee.group_id = t.id WHERE gender LIKE :ycp0 GROUP BY t.id) sq
> (/Applications/MAMP/htdocs/yiiroot/framework/db/CDbCommand.php:516)</p><pre>#0
> /Applications/MAMP/htdocs/yiiroot/framework/db/CDbCommand.php(411):
> CDbCommand->queryInternal('fetchColumn', 0, Array)
Model Code
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
$criteria->compare('gender',$this->gender,true);
$criteria->compare('agegroup',$this->agegroup,true);
$criteria->select = 't.*, IFNULL( count(tbl_attendee.id), 0) as attendeeCount';
$criteria->join = 'LEFT JOIN tbl_attendee ON tbl_attendee.group_id = t.id';
$criteria->group = 't.id';
$sort = new CSort();
$sort->attributes = array(
'attendeeCount'=>array(
'asc'=>'attendeeCount ASC',
'desc'=>'attendeeCount DESC',
),
'*', // add all of the other columns as sortable
);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>$sort,
));
View Code
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'group-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'name',
array(
'name'=>'gender',
'value'=>'Group::$genderLabels[$data->gender]',
'filter'=>Group::$genderLabels, // <-- This is the filter that breaks.
// $genderLabels = array('Female','Male);
),
Ah! I got it.
Changed this line in the model code:
$criteria->compare('gender',$this->gender,true);
to
$criteria->compare('t.gender',$this->gender,true);
Found the answer here.

Categories