Add an ActiveRecordInterface that allows using form models?
acorncom opened this issue · 2 comments
I'm dealing with a scenario where I do a POST to an endpoint (/api/user
) that is actually backed by two DB tables instead of one (see http://www.yiiframework.com/doc-2.0/guide-input-multiple-models.html for an example of what I'm trying to do).
In this scenario, defining a create or update controller action doesn't do what I'm after, as CreateAction.php#L42 and the following lines specifically assume a Yii2 ActiveRecord model instead of a Yii2 form model.
Could we define an JsonAPIActiveRecordInterface
that a form model could implement? Doing that would allow form models to return needed values while also letting app authors know that they would continue to work with the library create and update actions ...
Seems like we'd need to define the following methods in our interface ...
getPrimaryKey()
save()
- static
primaryKey()
findOne()
getRelation
unlinkAll
link
Basically CreateAction
and UpdateAction
cover only some basic scenarios on creating/updating a model. And they are implemented in the similar fashion with yii framework rest actions, so from what you've mentioned here is an example how it works by default in yii https://github.com/yiisoft/yii2/blob/master/framework/rest/CreateAction.php#L46
I would prefer to avoid creating this kind of interface (JsonAPIActiveRecordInterface
). First of all, I think there is no way to actually implement all of those methods in a form model. Also, this approach violates Interface Segregation Principle of SOLID.
Well, there are few approaches to deal with your scenario.
One of them is just to create a custom action that will be able to process multiple models. (the implementation could be similar to what is described in the link you've mentioned)
Thanks, that's helpful. Will handle it other ways 😀