Is there a way to prevent circular occurence in Data? #234
-
class FooData extends Data
{
public function __constructor(
public BarData $bar
)
{
}
public static function fromModel(Foo $model)
{
return new self(
BarData::from($model->bar)
);
}
} class BarData extends Data
{
public function __constructor(
public FooData $foo
)
{
}
public static function fromModel(Bar $model)
{
return new self(
FooData::from($model->foo)
);
}
} Update 2022-10-24 From the example above (or other similar situation) Foo and Bar model are bidirectional. $foo = Foo::find(1);
FooData::from(foo); // will loop forever It will keep looping until exhausted. How can I prevent it? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
In this case I would not use another If it still keeps looping, then take a look at the validation, and manually write out the rules or add a |
Beta Was this translation helpful? Give feedback.
In this case I would not use another
from
call in the custom creation methods since this will probably cause the loop. Manually creating the data object is the best option here.If it still keeps looping, then take a look at the validation, and manually write out the rules or add a
WithoutValidation
attribute to the property, since our validation rules resolver is probably creating nested validation rules loops.