Last active
August 29, 2015 14:00
-
-
Save vyspiansky/11380148 to your computer and use it in GitHub Desktop.
Yii: parameterized named scopes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Source: http://goo.gl/ZZrUWw | |
// See also: http://goo.gl/PCxJmO | |
public function relations() { | |
return array( | |
'rel' => array( | |
self::MANY_MANY, 'MyClass', 'table(id_1, id_2)', | |
'condition' => 'some conditions' | |
) | |
) | |
} | |
public function relByFieldName($fieldValue=5) { | |
$this->getDbCriteria()->mergeWith(array( | |
'with' => 'rel', | |
'condition' => 'field_name = :value', | |
'params' => array(':value' => $fieldValue) | |
)); | |
return $this; | |
} | |
// Usage: | |
$models = Model::model() -> relByFieldName(100) -> findAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment