want to add one more condition in findAll - yii

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

Related

Symfony Equal to and Not Equal to Criteria on Entity

i want to perform a Find operation on Entity Results.
$stores = c2gr($this, 'EducateToolsBundle:Stores')->findBy(['selectOption' => true, 'center_type_id' => 9])
i want to do center_type_id != 9
Just use a DQL or QueryBuilder.
$repository
->createQueryBuilder('s')
->where('s.selectOption = :selectOption')
->andWhere('s.center_type_id <> :center_type_id')
->setParameters([
'selectOption' => true
'center_type_id' => 9,
])
->getQuery()
->getResult();
You should be able to achieve that by using Criteria class. Something like this should work:
use Doctrine\Common\Collections\Criteria;
$criteria = Criteria::create();
$criteria->where($criteria->expr()->neq('center_type_id', 9));
$criteria->andWhere($criteria->expr()->eq('selectOption', true));
$entityRepository = $this->getDoctrine()->getRepository('C2EducateToolsBundle:Stores');
$result = $entityRepository->matching($criteria);
NOTE: above will work if you are in class which extends Symfony\Bundle\FrameworkBundle\Controller\Controller since it is using getDoctrine method. If you are not in such class you should either inject EntityManager or get it from service container.
For more details you should check this answer

CDBCriteria for Custom Cgridview

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

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

Hard time counting records using CDbCriteria

When I try to use CDbCriteria to count records I get Active record "Hotels" is trying to select an invalid column "count(t.id) as count".
Here is my code:
$criteria = new CDbCriteria();
$criteria->select = 'count(t.id) as `count`, t.`keywords`, min(offers.first_bookin) as first_bookin';
$criteria->with = array('offers');
$criteria->group = 'offers.hotels_id';
$criteria->compare('first_bookin','>' . date('Y-m-d'));
$criteria->together = true;
$hotelItems = Hotels::model()->findAll($criteria);
And here are my relations defined in Hotels model:
return array(
'headers' => array(self::HAS_MANY, 'Headers', 'hotels_id'),
'offers' => array(self::HAS_MANY, 'Offers', 'hotels_id'),
);
I red a lot of posts here and on some other sites, but nothing seems to work. I tried with count(*), count(id), I tried splitting the select property into an array. Every time I get the same error.
Just remove the backticks from the alias and the count should work. Of course if you want to access the count easily you will have to declare it as a class variable, as mentioned by onkarjanwa.
If you check the source of the error, it's this function: getColumnSelect of CActiveFinder, the select for count should satisfy this line, in getColumnSelect:
elseif(preg_match('/^(.*?)\s+AS\s+(\w+)$/im',$name,$matches)) // if the column is already aliased
but because of the backticks in the alias name, it doesn't match, and it throws an error. So your select should be without the backticks for aliases:
$criteria->select = 'count(t.id) as count, t.`keywords`, min(offers.first_bookin) as first_bookin';
When i was testing this, i got another error in your compare, so you should change it to:
$criteria->compare('offers.first_bookin','>' . date('Y-m-d')); // can't use the alias in where clause
You need to declare a variable $count because Yii does not automatically initiate variables that are not table columns, so you can't use any new variable without declaring that.
You can solve your problem by doing this.
Declare a model variable:
class Hotels extends CActiveRecord
{
public $count;
....
}

FuelPHP ORM Update via Array

The ORM in FuelPHP has an update example that looks like this:
$entry = Model_Article::find(4);
$entry->title = 'My first edit';
$entry->author = 'Total n00b';
$entry->save();
I'm wondering if there is an ability for me to update w/ something like this:
$values = array(
'this' => $that
);
Model_Article::find(4)->save($values);
The insert ability allows for passing arrays of values:
$props = array('property' => 'something');
$new = Model_Example::forge($props)->save();
But I see no example of doing the same thing w/ the update ability.
EDIT: It appears Model_Example::find(4)->values($array)->save(); is what I'm looking for.
It appears Model_Example::find(4)->values($array)->save(); is what I'm looking for.