V
V
Viktor Yanyshev2017-07-02 12:17:50
Yii
Viktor Yanyshev, 2017-07-02 12:17:50

Why is the model not loading after submitting data?

The site has a form in which there are several different models.
The form:

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'pagetitle')->textInput(['maxlength' => true]) ?>
<?= $form->field($collections, 'id')->dropDownList(\backend\models\Collections::find()
                        ->select(['name'])
                        ->indexBy('id')
                        ->column(), ['prompt'=>'Выберите коллекцию'])
                        ->label('Коллекция')
                        ->error(['text' => 'Это поле обязательное']) ?>
 <?= $form->field($image, 'images[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>

In controller:
$collections->load(Yii::$app->request->post('Collections'));
var_dump(Yii::$app->request->post('Collections'));
/* 
../ProductsController.php:89:
array (size=1)
  'id' => string '2' (length=1)
*/

var_dump($collections->id);
/*
../ProductsController.php:91:null
*/

Rules for the Collections model:
public function rules()
    {
        return [
            [['id'], 'required'],
            [['id'], 'integer'],
            [['name'], 'string', 'max' => 75],
        ];
    }

Only the id is sent from the form for Collections, a similar situation is that other models are not filled with their form data, why is the model not filled?
PS:
The controller generated by gii had this condition and saved the data in the database:
if($model->load(Yii:$app->request->post()) && $model->save())
{
  // Если убрать save(), и вызвать $model->pagetitle то null
  return $this->redirect(['view',  'id' => $model->id]);
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Arman, 2017-07-02
@Arik

What if it's simple?
By default, each form in its array and load collects its fields, i.e. fields that are relevant to the current form model. And you do $collections->id even though it's an array and you need collections['id']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question