2- معادل Yii :
class Post Extends CActiveRecord
{
...
public function relations()
{
return array(
'comments'=>array(self::HAS_MANY, 'Comment', 'post_id'),
'fooComments'=>array(selef::HAS_MANY, 'Comment', 'post_id', 'where'=>'title=:title', 'params'=>array(':title'=>'foo')),
);
}
...
}
// for use
$comments = Post::model()->findByPk(1)->comments;
$comments = Post::model()->findByPk(1)->fooComments;
البته اینطوری هم میشه نوشت:
class Post Extends CActiveRecord
{
...
public function relations()
{
return array(
'comments'=>array(self::HAS_MANY, 'Comment', 'post_id'),
);
}
...
}
// for use
$comments = Post::model()->findByPk(1)->comments;
$comments = Post::model()->findByPk(1)->comments(array('condition'=>'title=:title','param s'=>array(':title'=>'foo')));
حالا یک مثال جالب از Yii (میخوام ببینم لاراول برای معادلسازیش چطور عمل کرده). فرض کنید میخوایم تمام پستهایی رو پیدا کنیم که توی کامنتهاشون، عبارت PHP نوشته شده بوده:
class Post Extends CActiveRecord
{
...
public function relations()
{
return array(
'comments'=>array(self::HAS_MANY, 'Comment', 'post_id'),
);
}
...
}
// for use
$postsWithPHPComments = Post::model()->with('comments'=>array('contition'=>'LOWER(commen t) LIKE :comment','params'=>array(':comment'=>'%php%')))->findAll();