Yii использование компонентов и поведения

Как я могу сделать обобщенную функцию, которая используется каждой моделью и контроллерами, использующими компонент, а также поведение?

-1

Решение

//your behavior
class TestBehavior extends CActiveRecordBehavior
{
public $a = '';
public $b = '';

public function getA(){
return $this->a;
}

public function getB(){
return $this->b;
}
...
}

//in your model
public function behaviors(){
return array(
'TestBehavior' => array(
'class' => 'TestBehavior',
'a' => 'value a ',
'b' => 'value b',
),
//one more behavior
);
}
...
// use model behavior:
echo $model->TestBehavior->getA();

// in controller you can use attachBehavior() it will be like:
$this->attachBehavior('TestBehavior', array(
'class' => 'TestBehavior',
));

Надеюсь, это поможет вам.

0

Другие решения

Других решений пока нет …