Skip to content

Instantly share code, notes, and snippets.

@nadar
Last active August 13, 2017 11:35
Show Gist options
  • Save nadar/ae6aeefcad4d590848faca35a718cb28 to your computer and use it in GitHub Desktop.
Save nadar/ae6aeefcad4d590848faca35a718cb28 to your computer and use it in GitHub Desktop.
Dynamic Detach After Find Event
<?php
// Model
class MyText extends \yii\db\ActiveRecord
{
public $unserialize = true;
public function behaviors()
{
$behaviors = []
if ($this->unserialize) {
'unserializejson' => [
'class' => JsonUnserializeBehavior::class,
'fields' => ['text'],
]
}
return $behaviors;
}
}
// How to dynamic disable:
$model = MyText::findOne(1); // already triggered
// how to provide object context variable within static call.
// The only possibility would be to provide ActiveQuery function which binds parameters to ActiveRecord Like:
$model = MyText::find()->where(['id' => 1])->bind(['unserialize' => false])->one();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment