Using the fromModel method when the data collection is called #382
-
I have this data model: final class AdminCategoryData extends Data {
public function __construct(
#[DataCollectionOf(AdminComponentData::class)]
public ?DataCollection $components,
) {}
} Inside the final class AdminComponentData extends Data {
public function __construct(
public string $location,
) {}
public function fromModel (Component $component) : self {
return new self(
$component->getLocation(),
);
}
} When I call the FYI How do we make sure The reason I am doing this because I want to append that value only on that specific |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
For anyone that might run into the same challenge, this is how you fix it: final class AdminCategoryData extends Data
{
public function __construct(
#[DataCollectionOf(AdminComponentData::class)]
public ?DataCollection $components,
) {
}
public function fromModel(RemoteCategory $category): self
{
return new self(
AdminCategoryBrandData::collection($category->components->map(function ($component) {
return AdminComponentData::fromModel($component);
})),
);
}
} You can send |
Beta Was this translation helpful? Give feedback.
-
Hi,
and |
Beta Was this translation helpful? Give feedback.
For anyone that might run into the same challenge, this is how you fix it:
You can send
fromModel
data objects to the collection, you just have to loop/map through them on the parent.If any package developer see…