How to save new entity with nested entities included #224
-
For example, I have the Place model with I need to save newly created Place in my JSON API. There's a requirement that photos must contain at least 2 photo. How to send request to API with body containing nested PlacePhoto entities? As I remember, there's no implementation for this, and I need to override the serialize method of model?
I'm trying to serialize that model, but photos array is empty. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The default will send IDs so if your model doesn't have an ID it won't send anything. If you need to send nested models override the default serializer. In your place adapter: @override
Future<Map<String, dynamic>> serialize(T model,
{bool withRelationships = true}) async {
final map = await super.serialize(model, withRelationships: true);
map['photos'] = mapIdsIntoModels(map['photos']);
return map;
} To map IDs into models you can use the finders for example To get the photo adapter inside your place adapter, look it up in |
Beta Was this translation helpful? Give feedback.
The default will send IDs so if your model doesn't have an ID it won't send anything. If you need to send nested models override the default serializer. In your place adapter:
To map IDs into models you can use the finders for example
await photoAdapter.findOne(photoId, remote: false)
and then turn them into maps viaphotoAdapter.serialize(model)
.To get the photo adapter inside your place adapter, look it up in
adapters