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
48
48
}
49
49
}
50
50
```
51
- Model:
51
+ Defining models:
52
+
53
+ 1 ) Let's define ` User ` model and declare an ` articles ` relation
52
54
``` php
53
55
use tuyakhov\jsonapi\ResourceTrait;
54
56
use tuyakhov\jsonapi\ResourceInterface;
55
57
56
58
class User extends ActiveRecord implements ResourceInterface
57
59
{
58
60
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
+ }
59
93
}
60
94
```
61
95
Enabling JSON Input
You can’t perform that action at this time.
0 commit comments