New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
json and activerecord attribute #3495
Comments
can be resolved by |
@Ragazzo it is good for |
Using It's better to add a getter: |
@qiangxue by your logic I will get bidirectional changes. I can't imagine how to get away from rewrite attribute value (save, asArray...) |
@qiangxue why one should be worry if it is called @dizews in your case it should not be AR, it can be simple object, saving will require saving primary model and in |
return [
'load' => [
'class' => \yii\behaviors\AttributeBehavior::className(),
'attributes' => [
self::EVENT_AFTER_FIND => 'jsonstring',
self::EVENT_AFTER_UPDATE => 'jsonstring',
self::EVENT_AFTER_INSERT => 'jsonstring',
self::EVENT_INIT => 'jsonstring',
...
]
'value' => function ($event) {
if (is_string($this->jsonstring)) {
$this->jsonstring = new SubModel(['attributes' => json_decode($this->jsonstring)]);
}
}
],
'unload' => [
'class' => \yii\behaviors\AttributeBehavior::className(),
'attributes' =>[
...
],
'value' => function ($event) {
if ($this->jsonstring instanceof Model) {
$this->jsonstring = json_encode($this->jsonstring->attributes);
}
}
],
];
} |
also can validate submodel on before validate and set error on owner as array |
@Ragazzo The problem with @dizews If you don't want bidirectional changes, just don't implement the setter. |
@qiangxue yes, you right ) also as for bidirectional changes, why one should handle them, is not it the case when you should avoid hitting attribute directly, just use provided object as need? |
How about this approach class JsonBehavior extends ConverterBehavior
{
protected function convertToStoredFormat($value)
{
return json_encode($value);
}
protected function convertFromStoredFormat($value)
{
$array = json_decode($value);
return $this->arrayToObjectRecrusive($array);
}
private function arrayToObjectRecrusive($array)
{
foreach($array as $key=>$value){
if(is_array($value)){
$array[$key] = $this->arrayToObjectRecrusive($value);
}
}
return (object)$array;
}
} usage function behaviors()
{
return [
[
'class'=>JsonBehavior::className(),
'attributes'=>[
'jsonObject' => 'jsonstring'
]
],
]
}
// in application
echo $model->jsonObject->property; Is a another reason to put this behavior as official? :D |
@mdmunir thanks! |
@cebe exist a option for add this behavior for yii2 ? |
@spiritdead add what exactly? there are a lot of different proposals that cover specific cases |
Hello.
I have an attribute which contain a json string. I want to get an element of a json as a property of object.
example:
$model->position->lng
where$model->position
is a json string.It is a good idea to describe this example (or similar) in docs.
The text was updated successfully, but these errors were encountered: