Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use yii\base\Model;
- /*
- Мой класс наследуется от ComponentWithTwoDiffrentMethodsForBehaviors,
- В котором есть поддержка двух методов с поведениями.
- Как мне нужно
- */
- class NewModel extends ComponentWithTwoDiffrentMethodsForBehaviors
- {
- /*
- Здесь хранятся один набор поведений
- */
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::className(),
- 'createdAtAttribute' => 'created_at',
- 'updatedAtAttribute' => 'updated_at',
- ],
- ];
- }
- /*
- Здесь хранятся один набор поведений
- */
- public function behaviors2()
- {
- return [
- [
- 'class' => BlameableBehavior::className(),
- 'createdByAttribute' => 'author_id',
- 'updatedByAttribute' => 'updater_id',
- ],
- ];
- }
- }
- /*
- Моя примерная реализация данной задачи
- */
- class ComponentWithTwoDiffrentMethodsForBehaviors extends Model
- {
- public $ensureBehaviorsLock = false;
- public function behaviors()
- {
- return [];
- }
- public function behaviors2()
- {
- return [];
- }
- private function attachBehavior2Internal($name, $behaviors2)
- {
- return $this->attachBehavior($name, $behaviors2);
- }
- public function ensureBehaviors2()
- {
- foreach ($this->behaviors2() as $name => $behaviors2) {
- $this->attachBehavior2Internal($name, $behaviors2);
- }
- }
- public function ensureBehaviors()
- {
- if ($this->ensureBehaviorsLock) {
- return;
- }
- $this->ensureBehaviorsLock = true;
- parent::ensureBehaviors();
- $this->ensureRelations();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement