CDBCriteria for Custom Cgridview - yii

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'];

Related

Convert Yii1 code to Yii2

I am a newbie in Yii framework and I am trying to convert a Yii1 code to Yii2.
I know that there is no CDbCriteria class in Yii2, so for example, for the following Yii1 code:
$criOrder = new CDbCriteria();
$criOrder->order = 'CRI_PresentationOrder';
$criModels = Criteria::model()->findAll($criOrder);
I have produced the Yii2 statement:
$criModels = Criteria::find()->orderBy('CRI_PresentationOrder')->all();
But I have some problems trying to find the right way to convert this one:
$crvCriteria = new CDbCriteria();
$crvCriteria->with = array('aCRCRV', 'aCRCRV.cRVCRI');
$crvCriteria->together = true;
$crvCriteria->condition = 'ACR_APP_Id = :appId';
$crvCriteria->params = array(':appId'=>$id);
$crvCriteria->order = 'cRVCRI.CRI_PresentationOrder';
$crvModels = ApplicantCriteriaValue::model()->findAll($crvCriteria);
especially regarding the 'with', 'together' and 'params' attributes.
Any ideas/suggestions would be highly appreciated.
Assuming that your ApplicantCriteriaValue table has a model named ApplicantCriteriaValue then should be
$criModels = ApplicantCriteriaValue::find()
->join('INNER JOIN', 'tbl_criteria_value',
'tbl_criteria_value.ACV_CRV_Id =ApplicantCriteriaValue.CRV_Id')
->join('INNER JOIN', 'tbl_criteria',
'tbl_criteria.CRV_CRI_Id =ApplicantCriteriaValue.CRV_Id')
->orderBy('CRI_PresentationOrder')->all();

want to add one more condition in findAll

the query is
$model = Recipe::model()->findAll(array("condition" => "title like '$request%'"));
i want to add one more condition , status = Approved
is it good to add second condition in this findAll or use findallbyattributes and how to add second condition in this findAll
You could just add AND status = "Approved", but I think the best approach would be to use a CDbCriteria, like this:
$criteria = new CDbCriteria();
$criteria->addSearchCondition($request);
$criteria->compare('status', 'Approved');
$model = Recipe::model()->findAll($criteria);
What do you think?
This should simply work -
$model = Recipe::model()->findAll(array("condition" => "title like '$request%' and status = 'Approved'"));

Cdbcriteria Join - Column Not Found

I have the following SQL
SELECT entry_subject.id, entry_subject.subject_id
FROM entry_subject
INNER JOIN subject
ON entry_subject.subject_id = subject.id
WHERE subject.id = 71
I have arrived at the following which is an almost carbon copy of a number of examples, however I'm getting 1054 Unknown column 'EntrySubject.subject_id' in 'field list'.
$subjectEntries = new CDbCriteria();
$subjectEntries->alias = 'EntrySubject';
$subjectEntries->select = 'EntrySubject.id, EntrySubject.subject_id';
$subjectEntries->join = 'INNER JOIN Subject ON Subject.id = EntrySubject.subject_id';
$subjectEntries->condition = 'Subject.id=71';
$subjectEntriesModel=Subject::model()->findAll($subjectEntries);
My relationships are as follows
return array(
'entrySubject' => array(self::HAS_MANY, 'EntrySubject', 'subject_id'),
'phase' => array(self::BELONGS_TO, 'Phase', 'phase_id'),
'subjectType' => array(self::BELONGS_TO, 'SubjectType', 'subject_type'),
);
What am I doing wrong?
Many thanks
I assume you are trying to get subject entries for subject #71, then you should simply try the following :
$subject = Subject::model()->findByPk(71);
$subjectEntriesModel = $subject->entrySubject;
EDIT : you should, usually, avoid eager loading (as described in miog and Burhan Çetin answers) for a HAS_MANY relation
From soju's answer above
I assume you are trying to get subject entries for subject #71, then
you should simply try the following :
$subject = Subject::model()->findByPk(71);
$subjectEntriesModel = $subject->entrySubject;
I would suggest an addition to this.
$subject = Subject::model()->with('entrySubject')->findByPk(71);
$subjectEntriesModel = $subject->entrySubject;
->with('entrySubject') was added
This will allow yii to use one query instead if it is possible.
$query = Subject::model()->with('entrySubject')->findbypk($id);

how to get data from inner join using cdbcriteria in yii?

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

Yii update with join

I have a code
$command = Yii::app()->db->createCommand()
->update(
'queue q',
array('i.status_id' => $status_id)
)
->join('item i', 'q.item_id = i.item_id')
->where('IN', 'queue_id', $ids);
after I call $command->buildQuery() I get an error:
CDbCommand failed to execute the SQL statement: Invalid parameter number: parameter was not defined. The SQL statement executed was: UPDATE queue q SET i.status_id=:i.status_id
The impression is that it does not see the join and where commands.
What the problem?
Your code is valid with the newest Yii version. This MySQL-specific functionality has been added as of 1.1.14: https://github.com/yiisoft/yii/commit/ed49b77ca059c0895be17df5813ee1e83d4c916d.
The where clause should be in the update() function like this
Yii::app()->db->createCommand()
->update(
'queue q',
array('i.status_id' => $status_id),array('in', 'queue_id', $ids)
);
And regarding the JOIN part there is a open bug at https://github.com/yiisoft/yii/issues/124 (Im not sure. Correct me if Im wrong). Please let me know if there is a workaround.
You have to bind the parameters:
$command = Yii::app()->db->createCommand()
->update(
'queue q',
array('i.status_id' => ':status_id'),
array('in', 'queue_id', $ids),
array(':status_id' => $status_id),
)
->join('item i', 'q.item_id = i.item_id');
Having come across this problem a few times in my projects I have come-up with the following Yii work-around using CDbCriteria which is a little hacky, but gives the security of param count matching.
When applied to your example my code would be (guessing a little bit of your structure):
$ids = array(1,2,3,4,5);
$criteria = new CDbCriteria();
$criteria->addInCondition('i.queue_id',$ids);
$sql = '
UPDATE queue q
JOIN item i
ON q.item_id = i.item_id
SET i.status_id = :status
WHERE '.$criteria->condition;
$command = Yii::app()->db->createCommand($sql);
$command->bindValue('status',$status);
$command->bindValues($criteria->params);
$rows = $command->execute();