REST Relations - What am I doing wrong?

Hi everyone,

Im creating a project using REST and Im having some problems with relations.

First I read this article about best practices: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

On my project I have 2 models, Region and RegionComment.

When I request one GET on the URL ‘project/api/web/v1/regions’ everything works fine, I get all the results from the database.

Same happens when I request ‘region-comments’.

The article mentions that if I want to get everything from a Region, for example, I should request a GET on the following URL:

‘project/api/web/v1/regions/1/region-comments’

I dont know what Im doing wrong. What`s missing.

Here is some code:

Class Region:




class Region extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'region';

    }

    

    /**

     * @inheritdoc

     */

    public static function primaryKey()

    {

        return ['id'];

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['tag'], 'required'],

            [['total_status'], 'integer'],

            [['tag'], 'string', 'max' => 50]

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'tag' => 'Tag',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getRegionComments()

    {

        return $this->hasMany(RegionComment::className(), ['id_region' => 'id']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getRegionRelations()

    {

        return $this->hasMany(RegionRelation::className(), ['id_region_parent' => 'id']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getRegionRelations0()

    {

        return $this->hasMany(RegionRelation::className(), ['id_region_child' => 'id']);

    }

}



Class RegionComments:




class RegionComment extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'region_comment';

    }

    

    /**

     * @inheritdoc

     */

    public static function primaryKey()

    {

        return ['id'];

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['id_region', 'id_category', 'is_do', 'comment', 'total_status'], 'required'],

            [['id_region', 'id_category', 'is_do', 'total_status'], 'integer'],

            [['comment'], 'string', 'max' => 255]

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'id_region' => 'Id Region',

            'id_category' => 'Id Category',

            'is_do' => 'Is Do',

            'comment' => 'Comment',

            'total_status' => 'Total Status',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getIdCategory()

    {

        return $this->hasOne(Category::className(), ['id' => 'id_category']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getIdRegion()

    {

        return $this->hasOne(Region::className(), ['id' => 'id_region']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getStatusLogs()

    {

        return $this->hasMany(StatusLog::className(), ['id_region_comment' => 'id']);

    }

}



RegionController:




class RegionController extends ActiveController

{

    public $modelClass = 'api\modules\v1\models\Region';

}



I have exactly the same issue. Link to My Post.