File tree Expand file tree Collapse file tree 1 file changed +35
-1
lines changed
Expand file tree Collapse file tree 1 file changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -48,14 +48,48 @@ class Controller extends \yii\rest\Controller
4848 }
4949}
5050```
51- Model:
51+ Defining models:
52+
53+ 1 ) Let's define ` User ` model and declare an ` articles ` relation
5254``` php
5355use tuyakhov\jsonapi\ResourceTrait;
5456use tuyakhov\jsonapi\ResourceInterface;
5557
5658class User extends ActiveRecord implements ResourceInterface
5759{
5860 use ResourceTrait;
61+
62+ public function getArticles()
63+ {
64+ return $this->hasMany(Article::className(), ['author_id' => 'id']);
65+ }
66+ }
67+ ```
68+ 2 ) Now we need to define ` Article ` model
69+ ``` php
70+ use tuyakhov\jsonapi\ResourceTrait;
71+ use tuyakhov\jsonapi\ResourceInterface;
72+
73+ class Article extends ActiveRecord implements ResourceInterface
74+ {
75+ use ResourceTrait;
76+ }
77+ ```
78+ 3 ) As the result ` User ` model will be serialized into the proper json api resource object:
79+ ``` javascript
80+ {
81+ " data" : {
82+ " type" : " users" ,
83+ " id" : " 1" ,
84+ " attributes" : {
85+ // ... this user's attributes
86+ },
87+ " relationships" : {
88+ " articles" : {
89+ // ... this user's articles
90+ }
91+ }
92+ }
5993}
6094```
6195Enabling JSON Input
You can’t perform that action at this time.
0 commit comments