Answer the question
In order to leave comments, you need to log in
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>
<?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']);
}
}
<?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']);
}
}
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
In general, again Yii surprises
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question