Hello everyone.
Im getting this exception “Invalid Call – [yii\base\InvalidCallException] Setting read-only property: app\models\AsignaturaSearch::profesor” when I search data in the index view generated with Gii.
Here’s the code:
Model Asignatura.php
class Asignatura extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'asignatura';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['nombre'], 'required'],
[['id_profesor'], 'integer'],
[['nombre', 'descripcion'], 'string', 'max' => 45],
[['id_profesor'], 'exist', 'skipOnError' => true, 'targetClass' => Profesor::className(), 'targetAttribute' => ['id_profesor' => 'id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'nombre' => 'Nombre',
'descripcion' => 'Descripcion',
'id_profesor' => 'Id Profesor',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProfesor()
{
return $this->hasOne(Profesor::className(), ['id' => 'id_profesor']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getNotas()
{
return $this->hasMany(Nota::className(), ['id_asignatura' => 'id']);
}
}
Model AsignaturaSearch.php
class AsignaturaSearch extends Asignatura
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'id_profesor'], 'integer'],
[['nombre', 'descripcion', 'profesor'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Asignatura::find();
$query->joinWith(['profesor']);
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['profesor'] = [
// The tables are the ones our relation are configured to
'asc' => ['profesor.nombre' => SORT_ASC],
'desc' => ['profesor.nombre' => SORT_DESC],
];
if (!($this->load($params) && $this->validate()))
{
return $dataProvider;
}
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'id_profesor' => $this->id_profesor,
]);
$query->andFilterWhere(['like', 'nombre', $this->nombre])
->andFilterWhere(['like', 'descripcion', $this->descripcion])
->andFilterWhere(['like', 'profesor.nombre', $this->profesor]);
return $dataProvider;
}
}
Controller AsignaturaController.php
public function actionIndex()
{
if(Yii::$app->user->isGuest) {
return $this->redirect(Url::toRoute('site/login'));
}
$searchModel = new AsignaturaSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
view index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'nombre',
'descripcion',
['attribute' => 'profesor',
'value' => 'profesor.nombre'],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I don’t know where is the error. I hope anyone can help me, I’ll be forever gratefull.