This is my SQL. I want to create a CDbCriteria in Yii.
select us.user_id,u.clientid from user_session us
inner join user u on u.id=us.user_id
where us.auth_token='authtoken0000000001'
I tried this, but gives the wrong result. I also defined a relation related in my user_session model for this.
$criteria = new CDbCriteria;
$criteria->select = "user_id,user.clientid as client_id";
$criteria->condition='auth_token="'.$token.'"';
$clientIdarray = UserSession::model()->with('related')->find($criteria);
$dataprovider=New CActiveDataProvider('Bla',
array(
'criteria'=>array(
'order'=>'id ASC',
'with'=>array(
'user',
),
'joinType'=>'INNER JOIN',
'condition'=>'user.auth_token = '.$token,
)
));
Just an example how to do dataprovider init.
$criteria->with do what you need.
This topic will help. http://www.yiiframework.com/doc/guide/1.1/en/database.arr
Related
I need to conver this sql statement that has a multi column group by clause into CActiveDataProvider in Yii version 1.1.4?
select * from my_table_name group by bookname,categorytitle,bookkey order by bookname,categorytitle,bookkey asc;
I need a CActiveDataProvider to feed a CGridView search function. Apparently, I only have a simple return in my model of the search function that runs my CGridView
return new CActiveDataProvider(get_class($this),
array(
'criteria' => $criteria,
'sort' => array(
'defaultOrder' => 'id ASC'
),
'pagination' => array('pageSize' => ActiveRecord::PAGE_SIZE),
));
so how to convert the sql statement that I gave into CActiveDataProvider format ?
You can do using two way -
Make sql view and than create yii model and use this in your
controller or page view.
Create custom search function in your existing model and use group by
e.g. -
<?php
public function new_search(){
$criteria = new CDbCriteria();
...
..
$criteria->group = "bookname, categorytitle, bookkey";
$criteria->order = "bookname, categorytitle, bookkey";
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array('pageSize'=>100)
));
}
?>
I have a simple SQL query, which I need to convert to use in Yii 1.1.
SELECT *
FROM User
INNER JOIN Role ON Role.UserId = User.Id
WHERE Role.Name = 'admin'
How is this written into the CActiveDataProvider?
I have came up with an answer. Hopefully it helps someone in the future.
$dataProvider = new ActiveDataProvider('User', array
(
'criteria' => array
(
'with' =>'roles',
'join' => 'INNER JOIN Role r ON r.UserId = User.Id',
'condition' => 'r.Name=:term',
'params' => array(':term'=>'admin')
)
));
I do not fully know yii framework. How to make CDbCriteria for cases like this?
I have a basic query code like this.
SELECT
jam_kerja.id,
jam_kerja.id_cabang,
jam_kerja.tgl_berlaku,
jam_kerja_detail.id_jam_kerja,
jam_kerja_detail.shift,
jam_kerja_detail.jamkerja,
jam_kerja_detail.jamistirahat
FROM
jam_kerja ,
jam_kerja_detail
WHERE
jam_kerja_detail.id_jam_kerja = jam_kerja.id and jam_kerja.id_cabang=5
Maybe there Yii friends who can help me?
$criteria = new CDbCriteria();
$criteria->select = 't.id,t.id_cabang, t.tgl_berlaku, jd.id_jam_kerja, jd.shift ,jd.jamkerja,';
$criteria->join = 'INNER JOIN jam_kerja_detail jd ON t.id = jd.id_jam_kerja';
$criteria->addCondition('t.id_cabang=5'); // edited this line
//and now give this criteria to your model
$model = YourModel::model()->findAll($criteria);
I finally found the solution, it may be useful to other friends.
$criteria=new CDbCriteria;
$criteria->join='INNER JOIN jam_kerja t1 ON t1.id=t.id_jam_kerja';
$criteria->condition='t1.id_cabang='.$_GET['id'];
How can I execute this query with CDbCriteria and CActiveDataProvider?
'SELECT * FROM tbl_post where title LIKE %'.$title.'% ORDER BY title LIKE '.$title.' DESC , title LIKE '.$title.'% DESC'
update:
finaly I wrote this:
$criteria = new CDbCriteria;
$criteria->addCondition('title LIKE :title');
$criteria->params = array(':title'=>'%'.$title.'%',':t1'=>$title,':t2'=>$title.'%');;
$criteria->order='title LIKE :t1 DESC , title LIKE :t2 DESC';
but I got error:
CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens. The SQL statement executed was: SELECT COUNT(*) FROM `tbl_post` `t` WHERE title LIKE :title
Try using compare clause, not sure is it you want with the 'order by x like' :
<?php
$criteria = new CDbCriteria;
$criteria->compare('title', $title, true);
$criteria->order = 'title DESC';
$dp = new CActiveDataprovider('posts', array(
'criteria' => $criteria
));
This can be achieved like this:
$dataProvider=new CActiveDataProvider('tbl_post', array(
'criteria'=>array(
'condition'=>'title LIKE % '.$title.' %',
'order'=>'title DESC',
),
));
$dataProvider->getData();
also refer : Help
Thanks.
I have the following example code:
$dataProvider = new CActiveDataProvider('firstTable',
array('criteria' => array(
'select' => 't.firstfield,secondTable.secondfield',
'join' => 'join secondTable on secondTable.id=t.secondTable_id',
),
'pagination' => array(
'pageSize' => 10,
),
));
$results=$dataProvider->getData();
After running the code above, firstField (from the model table - firstTable) is available in the object, but secondField (from the joined table - secondTable) is not.
Can anyone provide assistance on what is wrong with the code or why the "select" option is not picking up the secondField?
it would be better if you use CDbCriteria, that has a better solution to join table with the help of relations. I can show the example with CDbCriteria.
$criteria = new CDbCriteria;
$criteria->select = 'firstfield';
$criteria->with = array('secondTable_relation'=>array('select'=>'secondfield'));
$dataProvider = new CActiveDataProvider('firstTable',
array('criteria' => $criteria,
'pagination' => array(
'pageSize' => 10,
),
));
$results=$dataProvider->getData();
secondTable_relation is a relation name with secondTable.
Can anyone provide assistance on what is wrong with the code or why the "select" option is not picking up the secondField?
Answer:
That is happening because you have not selected the field which relates the two tables, i.e the foreign key in firstTable : secondTable_id. So if you do:
'select' => 't.firstfield,t.secondTable_id,secondTable.secondfield',
you will be able to access the secondField of secondTable:
$singleresultrow->secondTableRelationName['secondField'];// a single result row can be obtained by foreach iteration over $results
However there will still be another query (Lazy Loading) when you access the secondField. And the initial(before lazy) object returned will not have the secondTable object filled.
The problem i think lies in the fact that by default yii accesses the related fields by lazy loading, and for that to happen the related foreign_key should be present in the model you are trying to make a dataprovider of, here it is firstTable, and the foreign_key secondTable_id.