E
E
Eugene2018-04-25 10:34:39
Yii
Eugene, 2018-04-25 10:34:39

How to cure Yii2 dropDownList selection?

In general, again Yii surprises. Editing the creation form. Everything was created by KRAD.
I have tables with links in the database, so KRAD immediately generated links in the models.
Instead of the category number, I wanted to display the selection of the department according to the associated table.
In the form view I did

<div class="price-form">

    <?php $form = ActiveForm::begin(); ?>


    <?= $form->field($model, 'category_id')->dropDownList(ArrayHelper::map(Category::find()->all(), 'id','name'))?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'unit')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'price')->textInput(['maxlength' => true]) ?>

    <div class="form-group">
        <?= Html::submitButton(Yii::t('app', 'Сохранить'), ['class' => 'btn btn-style-one']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

It does work. You can choose a category. And you can even change. But the input itself is empty. It is not clear which parameter was originally, and which one you chose. What kind of sorcery is this? I did the exact same thing on another project.
Price model
<?php

namespace app\modules\admin\models;

use Yii;

/**
 * This is the model class for table "price".
 *
 * @property int $id Номер
 * @property int $category_id Номер категории услуги
 * @property string $name Наименование услуги
 * @property string $unit Единица измерения
 * @property string $price Цена в рублях
 *
 * @property Orders[] $orders
 * @property Category $category
 */
class Price extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'price';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['category_id'], 'integer'],
            [['name'], 'string', 'max' => 255],
            [['unit'], 'string', 'max' => 60],
            [['price'], 'string', 'max' => 30],
            [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '№',
            'category_id' => 'Отделение',
            'name' => 'Наименование услуги',
            'unit' => 'Единица измерения',
            'price' => 'Цена',
        ];
    }

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

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(Category::className(), ['id' => 'category_id']);
    }
}
Category Model
<?php

namespace app\modules\admin\models;

use Yii;

class Category extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'category';
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[['description', 'keywords'], 'string'],
[['name'], 'string', 'max' => 30],
];
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'description' => 'Description',
'keywords' => 'Keywords',
];
}

/**
* return \yii\db\ActiveQuery
*/
public function getDoctors()
{
return $this->hasMany(Doctors::className(), ['category_id' => 'id']);
}

/**
* return \yii\db\ActiveQuery
*/
public function getOrders()
{
return $this->hasMany(Orders::className(), ['category_id' => 'id']);
}

/**
* return \yii\db\ActiveQuery
*/
public function getPrices()
{
return $this->hasMany(Price::className(), ['category_id' => 'id']);
}
}

But visually there is no active selection in the input.
5ae035bc96daf397958170.jpeg
Could this be related to the rules?
It just generated such rules
public function rules()
    {
        return [
            [['category_id'], 'integer'],
            [['name'], 'string', 'max' => 255],
            [['unit'], 'string', 'max' => 60],
            [['price'], 'string', 'max' => 30],
            [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],
        ];
    }

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2018-04-25
@webinar

In general, again Yii surprises

I’m ready to argue that the layout is surprising, some kind of js, but yii definitely has nothing to do with it. Open the console and look for errors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question