Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active August 29, 2015 14:00
Show Gist options
  • Save vyspiansky/11380148 to your computer and use it in GitHub Desktop.
Save vyspiansky/11380148 to your computer and use it in GitHub Desktop.
Yii: parameterized named scopes
<?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