Yii2: ActiveRecord function fields() & getLinks() does not work

Created on 14 Mar 2015  路  7Comments  路  Source: yiisoft/yii2

I am using YII2 Rest API. I have a model class named Editorial which extends yiidbActiveRecord and implements Linkable Interface.

the fields method is as follows

public function fields() {        
        return [
            'id',
            'title',
            'description',
            'user_id' => function(){
                return $this->user;
            },
            'created' => function(){
                return date('Y-m-d H:i:s',  $this->created);
            },
            'updated' => function(){
                return date('Y-m-d H:i:s',  $this->created);
            }
        ];
    } 

public function getLinks(){
        return [
            Link::REL_SELF => Url::to(['/editorial/view', 'id' => $this->id], true)
        ];
    }

I do have relations

/**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEditorialComments()
    {
        return $this->hasMany(EditorialComments::className(), ['editorial_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEditorialDislikes()
    {
        return $this->hasMany(EditorialDislikes::className(), ['editorial_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEditorialLikes()
    {
        return $this->hasMany(EditorialLikes::className(), ['editorial_id' => 'id']);
    }

When I use the following query the fields and getlinks method does not work

Editorial::find()
                ->with('user',
                    'editorialComments',
                    'editorialComments.user',
                    'editorialLikes',
                    'editorialDislikes')
                ->orderBy('created DESC')
                ->asArray()
                ->all()

When i make use of ActiveDataprovider in my controller which extends yii\rest\Controller then the relations are not displayed although fields and getLinks function works correctly.

return ActiveDataProvider([
    'query' => Editorial::find()
                ->with('user',
                    'editorialComments',
                    'editorialComments.user',
                    'editorialLikes',
                    'editorialDislikes')
                ->orderBy('created DESC')
]);
rest

Most helpful comment

If I remove ->asArray() method from the following
Editorial::find()
->with('user',
'editorialComments',
'editorialComments.user',
'editorialLikes',
'editorialDislikes')
->orderBy('created DESC')
->asArray()
->all()

then fields and getLinks methods works correctly, but none of the relation is shown and this is also a problem with ActiveDataprovider.

-asArray() shows all the relations but fields and getLinks methods does'nt work and vice-versa.

What is the correct way to show the relations data and fields + links method ?

All 7 comments

If I remove ->asArray() method from the following
Editorial::find()
->with('user',
'editorialComments',
'editorialComments.user',
'editorialLikes',
'editorialDislikes')
->orderBy('created DESC')
->asArray()
->all()

then fields and getLinks methods works correctly, but none of the relation is shown and this is also a problem with ActiveDataprovider.

-asArray() shows all the relations but fields and getLinks methods does'nt work and vice-versa.

What is the correct way to show the relations data and fields + links method ?

can you edit your code and use a markdown syntax? it's hard to read

I am using YII2 Rest API. I have a model class named Editorial which extends yiidbActiveRecord and implements Linkable Interface.

The fields method is as follows

public function fields() {        
        return [
            'id',
            'title',
            'description',
            'user_id' => function(){
                return $this->user;
            },
            'created' => function(){
                return date('Y-m-d H:i:s',  $this->created);
            },
            'updated' => function(){
                return date('Y-m-d H:i:s',  $this->created);
            }
        ];
    } 
public function getLinks(){
        return [
            Link::REL_SELF => Url::to(['/editorial/view', 'id' => $this->id], true)
        ];
    }
I do have relations
     /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEditorialComments()
    {
        return $this->hasMany(EditorialComments::className(), ['editorial_id' => 'id']);
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEditorialDislikes()
    {
        return $this->hasMany(EditorialLikesDislikes::className(), ['editorial_id' => 'id'])->where(['status' => Editorial::STATUS_LIKE]);
    }
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEditorialLikes()
    {
        return $this->hasMany(EditorialLikesDislikes::className(), ['editorial_id' => 'id'])->where(['status' => Editorial::STATUS_DISLIKE]);
    }
When I use the following query the fields and getlinks method does not work
Editorial::find()
                ->with('user',
                    'editorialComments',
                    'editorialComments.user',
                    'editorialLikes',
                    'editorialDislikes')
                ->orderBy('created DESC')
                ->asArray()
                ->all()
When i make use of ActiveDataprovider in my controller which extends yii\rest\Controller then the relations are not displayed although fields and getLinks function works correctly.
return ActiveDataProvider([
    'query' => Editorial::find()
                ->with('user',
                    'editorialComments',
                    'editorialComments.user',
                    'editorialLikes',
                    'editorialDislikes')
                ->orderBy('created DESC')
]);
If I remove ->asArray() method from the following 
Editorial::find()
->with('user',
'editorialComments',
'editorialComments.user',
'editorialLikes',
'editorialDislikes')
->orderBy('created DESC')
->asArray()
->all()
then fields and getLinks methods works correctly, but none of the relation is shown and this is also a problem with ActiveDataprovider.
-asArray() shows all the relations but fields and getLinks methods does'nt work and vice-versa.
What is the correct way to show the relations data and fields + links method ?

ok let's get this sorted out, as a start you don't want to call asArray because obviously you will lose AR capabilities.
try declaring the relation names under extraFields

I can do this but always i do not want to fetch the relations. can you please suggest any other way ?

@dhiman252 extraFields does not mean that it will fetch the relations all the time, it will be only called if you pass it as an argument in the toArray call unless you already have it eager loaded which your code looks like it does

see the comments of @fernandezekiel

Was this page helpful?
0 / 5 - 0 ratings