Correct setup for a simple CDbCriteria in Yii? - php

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

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);

Yii CDbCriteria- Query using IN

What is the equivalent CDbCriteria method or properties if
i want to query this way
select * from tbl_data where id in(2,3,6,8)
Im not sure as to which property to use though.
Thanks in advance
Try This
$condInArr = array(2,3,6,8);
$Criteria = new CDbCriteria();
$Criteria->addInCondition('id', $condInArr);
$List = TblData::model()->findAll($Criteria);

Why does not work CActiveDataProvider after change criteria

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.

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);

Categories