Why does not work CActiveDataProvider after change criteria - php

This code work fine:
$criteria = new CDbCriteria;
$criteria->compare('id', 1);
$dataProvider = new CActiveDataProvider('User', array('criteria'=>$criteria));
foreach ($dataProvider->getData() as $value) var_dump($value->id);
But when I change criteria after create CActiveDataProvider instance this does not work:
$criteria = new CDbCriteria;
$criteria->compare('id', 1);
$dataProvider = new CActiveDataProvider('User', array('criteria'=>$criteria));
$criteria->compare('id', 2);
foreach ($dataProvider->getData() as $value) var_dump($value->id);
This return empty!
Why is this?

You don't have any result since you are adding a condition to your criteria : generated SQL will look like this : WHERE id='1' AND id='2'
If you want to list user 1 and 2 you should try :
$criteria->compare('id', 2, false, 'OR');
Or simply use addInCondition.
EDIT : as you said in your comment, instead of using the same criteria object, you can clone it.

Related

Convert yii1 query to yii2

I am new to yii1. I would like to convert this below query to yii2. May I know the difference between condition and param in yii2?
$criteria = new CDbCriteria();
$criteria->select = 'amount, name, level';
$criteria->condition = "account_type = :account_type";
$criteria->params=(array(':account_type'=>'credit'));
$result = $this->model()->find($criteria);
I have tried the below sample. But it's not giving me the same return as yii1 query.
$result = Model::find()
->select(['amount, name, level'])
->where(['account_type' => $account_type, 'account_type' => 'credit'])
->one();
Try this:
$result = Model::find()
->select(['amount, name, level'])
->where(['account_type' => 'credit'])
->one();

Find by pk with in condition

How can i findbypk with in condition
my query is
$model = User::model->findBypk($id);
now i want to apply In condition that UserRole should be in (1,2)
You can try this code :
$Criteria = new CDbCriteria();
$criteria->compare('id',$id);
$criteria->addInCondition('userRole', array (1,2));
$models = User::model()->findAll($Criteria);
You could use a criteria
criteria = new CDbCriteria();
$criteria->addInCondition('id', array(1,2));
$resultModels = User::model()->findAll($criteria);
The $resultModels return a collection of models that contain the required rows
Or if you want single model you can use find
criteria = new CDbCriteria();
$criteria->addInCondition('id', array(1,2));
$model = User::model()->find($criteria);

Correct setup for a simple CDbCriteria in Yii?

This fails when I use $currentUser->id in my CollectionController.php
$criteria = new CDbCriteria();
$criteria->condition = 'tbl_userId=:tbl_userId';
$criteria->params = array(':tbl_userId' =>$currentUser->id);
$dataProvider=new CActiveDataProvider('Collection', array('criteria'=>$criteria));
But if I just plug a literal in the params array my query comes back correct.
$criteria = new CDbCriteria();
$criteria->condition = 'tbl_userId=:tbl_userId';
$criteria->params = array(':tbl_userId' => 12);
$dataProvider=new CActiveDataProvider('Collection', array('criteria'=>$criteria));
What's the quickest Yii-way to verify my var? $currentUser->id
I'm new to PHP / Yii... Thanks for your help.
echo out $currentUSer->id from the controller. Don't execute the query that is failing and the view should render with what $currentUser->id is. Nothing Yii style, but it's a quick way to find out what if anything the $currentUser is returning

Count Relational Table Entries

Code:
$criteria = new CDbCriteria;
$criteria->select='id, name, count(events.id) AS event_relational';
$criteria->with=array('events');
$model = Profiles::model()->findAll($criteria);
Response:
[{"id":"1","name":"Profile 1","event_relational":"0"}]
Code 2:
$criteria = new CDbCriteria;
$criteria->select='id, name';
$criteria->with=array('events');
$model = Profiles::model()->findAll($criteria);
Response 2:
[{"id":"1","name":"Profile 1","event_relational":null},{"id":"2","name":"Profile 2","event_relational":null}...]
When I use COUNT the query stop in the first MySQL entry.
I would like to the code return all entries and check if have relational event.
Response that I want:
[{"id":"1","name":"Profile 1","event_relational":"0"},{"id":"2","name":"Profile 2","event_relational":"1"}...]
To do this, you will need to group by the profile.id :
$criteria = new CDbCriteria;
$criteria->select='id, name, count(events.id) AS event_relational';
$criteria->with=array('events');
$criteria->group='t.id';
$model = Profiles::model()->findAll($criteria);

Yii AR join not working when using findAll

I have two tables menus and lang_menus. My Menus model is as follows :
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'menulanguages'=>array(self::HAS_MANY, 'MenuLangs', 'menuId'),
);
}
...
public function getMenus(){
$criteria = new CDbCriteria();
$criteria->condition = "t.clientId = ".Yii::app()->user->clientId." AND menulanguages.languageId = ".Yii::app()->user->userlanguage;
$count = Menus::model()->with('menulanguages')->count($criteria);
$pages=new CPagination($count);
//Results per page
$pages->pageSize=10;
$pages->applyLimit($criteria);
$menus = Menus::model()->with('menulanguages')->findAll($criteria);
return array('menus' => $menus, 'paging' => $pages);
}
This is throwing the error Unknown column 'menulanguages.languageId'.
The error is in the line $menus = Menus::model()->with('menulanguages')->findAll($criteria);.
Surprisingly I am getting the value of the variable $count correctly.
On looking at the log I can see that the SQL query that is running for the findAll query is :
SELECT `t`.`id` AS `t0_c0`, `t`.`clientId` AS `t0_c1`, `t`.`restaurantId` AS `t0_c2` FROM `posif_menus` `t` WHERE (t.clientId = 1 AND menulanguages.languageId = 2) LIMIT 10
which means the join has not taken place. Whereas proper join query is running for the count value. Am I doing something wrong ?
Please help.
Just use together() of CActiveRecord or together property of CDbCriteria:
$menus = Menus::model()->with('menulanguages')->together()->findAll($criteria);
or:
$criteria = new CDbCriteria();
$criteria->condition = "t.clientId = ".Yii::app()->user->clientId." AND menulanguages.languageId = ".Yii::app()->user->userlanguage;
$criteria->together=true;
Try this.. apply joining table in cdbcriteria..
public function getMenus(){
$criteria = new CDbCriteria();
$criteria->condition = "t.clientId = ".Yii::app()->user->clientId." AND menulanguages.languageId = ".Yii::app()->user->userlanguage;
$criteria->with = array('menulanguages');
$count = Menus::model()->count($criteria);
$pages=new CPagination($count);
//Results per page
$pages->pageSize=10;
$pages->applyLimit($criteria);
$menus = Menus::model()->findAll($criteria);
return array('menus' => $menus, 'paging' => $pages);
}

Categories