What if my models are not backed by a relational database but by a restful web service?
What should I do so that all CRUD operations be performed through calling a web service, and bind UI elements to that web service operations?
Thanks.
What if my models are not backed by a relational database but by a restful web service?
What should I do so that all CRUD operations be performed through calling a web service, and bind UI elements to that web service operations?
Thanks.
Actually, I want to join to this question, also.
I have the same situation, all the data layer is hidden by the REST service layer.
What is the best way how to "marry" Yii2 models (AR model?) with external resource instead of direct connection to the database?
I think you should start with yii\base\Model.
Yes, I already suggested that, however if for example I’ll decide to use Paginator it will not work with classes extended from Model, so it seems I have to create a class something like “MyActiveRecord” that extends ActiveRecord and override all methods like “find, update, save, and so on…”. But this way seems not optimal and pretty hard.
So, I’m in search for the best practice of how it could/should be implemented.
Oh yes, we didn’t agreed in what approach you want to use WS.
If you just want to display the data from WebService there may be no need to convert it to Yii specific models. Yii data widgets (DetailView, GridView) works well with arrays, so you can use datatypes that WS offers. Pagination also doesn’t needs AR, for example this simple code paginates the ABC.
// controller action
public function actionTest()
{
// get the data
$arrayToPaginate = range('A', 'Z');
$pages = new Pagination(['pageSize'=>5, 'totalCount' => count($arrayToPaginate)]);
$models = array_slice($arrayToPaginate, $pages->offset, $pages->limit);
return $this->render('test', [
'models' => $models,
'pages' => $pages,
]);
}
// view
<div><?= implode($models); ?></div>
<div><?= LinkPager::widget(['pagination' => $pages,]); ?></div>
If you want to pass for WS data collected from user, then you may want to validate user input - use Model.
If you want to save data from WS to your local DB, you may extend WS offered datatypes with AR or assign properties to AR models.