Hello,
I am still trying to grasps the concept of Yii2,
I have created 2 models and their CRUDS called categories and posts respectively.
I am ok to pass in the Posts _form the categories via a drop down menu and save the whole form into the CRUD.
Where I am confused in this concept is that in MYSQL, I created a relation between the Categories and the posts as:
Categories has many posts.
But, the CRUD created in GII expect to have an ID of the categories in the _form of the posts…
How to get the categories name instead while still having the validation accepting the input?
Right now in my form I have this:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use frontend\models\categories;
use yii\models\user;
use yii\web\IdentityInterface;
/* @var $this yii\web\View */
/* @var $model frontend\models\Posts */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="posts-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => 160]) ?>
<?= $form->field($model, 'posts')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'keywords')->textInput(['maxlength' => 45]) ?>
<?= $form->field($model, 'categories_id')->dropDownList(
ArrayHelper::map(Categories::find()->all(),'id','id'),
['prompt'=>'Select a category']) ?>
<?= $form->field($model, 'user_id')->textInput(['value' => \Yii::$app->user->identity->id]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
But i do not want the ID to be output, but instead the name of the categories so instead I added this:
<?= $form->field($model, 'categories')->dropDownList(ArrayHelper::map(Categories::find()->all(),'id','id'),
['prompt'=>'Select a category']) ?>
This correctly shows the category names but won;t let me submit the form.
So I then added in the Posts model this:
public function rules()
{
return [
[['title', 'posts', 'categories_id', 'user_id'], 'required'],
[['posts'], 'string'],
[['created_at', 'updated_at'], 'safe'],
[['categories_id', 'user_id'], 'integer'],
[['title'], 'string', 'max' => 160],
[['keywords'], 'string', 'max' => 45],
[['categories'], 'string', 'max' => 64],<------------
];
}
I can submit the form now but right after submission I get this error:
Invalid Call – yii\base\InvalidCallException
Setting read-only property: frontend\models\Posts::categories
1. in C:\xampp\htdocs\forum\vendor\yiisoft\yii2\base\Component.php at line 195
186187188189190191192193194195196197198199200201202203204 foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = $value;
return;
}
}
}
if (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
/**
* Checks if a property value is null.
* This method will check in the following order and act accordingly
I am not getting this, could you please help me?
Thank you,
Ben