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);
Related
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);
I have a table Policy which links to another table Currency and the relation is as below:
'policyCurrency' => array(self::MANY_MANY, 'Currency', 'policy_currencies(PolicyId, CurrencyId)’),
I am also using the search function provided in the model to get the data and I want to select the Currency which resides in the related Currency Table as part of the search. Below is my search function.
public function search(){
$criteria = new CDbCriteria;
$criteria->compare('PolicyId', $this->PolicyId);
$criteria->compare('Name', $this->Name, true);
$criteria->compare('Amount', $this->Amount, true);
return new CActiveDataProvider($this, array( 'criteria' => $criteria));
}
When using the search getData:
$result = Policy->search()->getData();
The result returned is in the format of:
PolicyId = 1
Name = 'Test'
Amount = '20'
etc etc etc
I want the data related to the relations which is currency in this case be returned as part of the select like this (appending one after the other separated by a ',' since this is a MANY to MANY relation):
CurrencyId = 1,2,3
What I have tried:
Trying to use the
$criteria->with = array('policyCurrency');
$criteria->together = true;
but i am confused about how to use select to select all records from the first table Policy in my case and all related CurrencyIds seperated by a ',' from the related model.
Thanks
You need to add the relation name to the with attribute so it does a JOIN with Currency and gets all its related data:
public function search(){
$criteria = new CDbCriteria;
$criteria->with = array('policyCurrency');
For more info, check CDBCriteria documentation.
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
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.
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);
}