-
Hello, My use case class Foo extends Data
{
public function __construct(
#[DataCollectionOf(Bar::class)]
public DataCollection $bars
) {}
} class Bar extends Data
{
public function __construct(
public int $id,
public Lazy|string $title,
public Lazy|string $description
) {}
public function fromModel(BarModel $model){
return new self(
$model->id,
Lazy::create(fn() => $model->title)->defaultIncluded(),
Lazy::create(fn() => $model->description),
);
}
} ...
return new JsonResponse(
new Foo(
Bar::collection(BarModel::all())->include('description')
)
); The returned json looks like
From my debugging, the issue seems to occure when the One obvious solution would be to use new Foo(
Bar::collection(BarModel::all())->include('description')->toArray()
) but this would lose the typing so it's not really a solution. Is this by design or it's a bug? Is there a way to achieve what I'm trying and keep the typing as well? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Ok, so after a lot of digging through the package code, it seems that the proper solution is the following: return new JsonResponse(
(new Foo(
Bar::collection(BarModel::all())
))->include('bars.description')
); ExplanationIt seems that when a In my example, because I was including properties into The solution is to call the include on the
|
Beta Was this translation helpful? Give feedback.
Ok, so after a lot of digging through the package code, it seems that the proper solution is the following:
Explanation
It seems that when a
Data
is transformed (toArray
,toJson
,jsonSerialized
, etc), theDataCollection
properties are "flattened". This means that instead of treating the partial tree of each individualDataCollection
(Bar
) from the transformedData
(Foo
), only the partial tree of the bigData
(Foo
) will be treated. The containedDataCollection
of theData
will be flattened as sub-properties.In my example, because I was including properties into
Bar
, I wa…