Yii2 Invalid Call: Setting read-only property

Dan

I have a Post model that has a many-many relationship with Tags.

Defined in Post model:

public function getTags(){
    return $this->hasMany(Tags::className(), ['id' => 'tag_id'])
        ->viaTable('post_tags', ['post_id' => 'id']);
}

But Post::tags is read-only. So when I try to set them in the Controller, I get an error:

Invalid Call – yii\base\InvalidCallException

Setting read-only property: app\models\Post::tags

The controller is using load to set all the properties:

public function actionCreate(){
    $P = new Post();
    if( Yii::$app->request->post() ){
        $P->load(Yii::$app->request->post());
        $P->save();
        return $this->redirect('/posts');
    }
    return $this->render('create', ['model'=>$P]);
}

The input field in the view:

<?= $form->field($model, 'tags')->textInput(['value'=>$model->stringTags()]) ?>

Why is Post::tags read-only? And whats the proper way to set a model relationship?

SohelAhmedM

Here tags

public function getTags(){
    return $this->hasMany(Tags::className(), ['id' => 'tag_id'])
        ->viaTable('post_tags', ['post_id' => 'id']);
}

is a relation name and returns the object and is not just a attribute or database column.

You cannot use it with textInput() like other attribute for eg email, first_name.

So you are given error of Setting read-only property.

In order to remove this error, you need to create new attrbute or property to model like below

class Post extends \yii\db\ActiveRecordsd
{
    public $tg;
    public function rules()
    {
        return [
            // ...
            ['tg', 'string', 'required'],
        ];
    }
    // ... 

In view file

<?= $form->field($model, 'tg')->textInput(['value'=>$model->stringTags()]) ?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Yii2 Setting read-only property

"Invalid property assignment:.. read-only property" in Qml

Invalid property assignment: "anchors" is a read-only property

Eerror in social login in Yii2: Setting unknown property: yii\web\Application::authClientCollection

Yii2 Setting unknown property: yii\filters\auth\HttpBearerAuth::formats

Yii2 migrate Setting unknown property: yii\caching\FileCache::backuprestore

Property 'item' is 'Read Only'

Yii2 Login from DB (Setting unknown property: app\models\User::password_hash)

Cannot read property '$invalid' of undefined

yii2 Error of dynamic client validation URL - Cannot read property 'test' of undefined

Cannot read property 'call' of undefined

Angular2 - Cannot read property 'subscribe' of undefined in nested call

Angular2, call service function, Cannot read property '***' of undefined

call-interactively: Text is read-only: "Type `e' to edit property"

Python @property setter not setting from instance call

Call method upon setting a declared public property

Property is not being written to, only read from in Delphi XE2

Cannot assign to read only property

Python read-only property

DataBinding with read-only property

Data object property is read only

Invalid System.register call when setting up Karma with angular2

override read only property to make it read write

Setting Property using only string name

Setting a get-only property on an object in a collection

TypeError: "setting getter-only property "value"

Setting a file to "Read-Only" in Windows with ICACLS

Setting an NTFS file to be read only from Linux

Yii2: Setting layout for a specific controller