Answer the question
In order to leave comments, you need to log in
How to add a record to linked tables in yii2?
I have three tables
category shop shop_value
id id id
title id_value name
id_categor
They are connected like this
id_categor->id(category table)
id_value ->id(shop_value table)
it is necessary that when adding a record to the shop_value table,
I tried to make shop connect them, but apparently somewhere there is an error
model shop_value
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "shopvalue".
*
* @property integer $id_shop_value
* @property string $val
* @property string $img
* @property string $link
*
* @property Shop[] $shops
*/
class Shopvalue extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'shopvalue';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['shop_categor_list'], 'safe'],
[['val', 'img', 'link'], 'required'],
[['val', 'link'], 'string', 'max' => 500],
[['img'], 'file', 'extensions'=>['jpg','png']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id_shop_value' => 'Id Shop Value',
'val' => 'Val',
'img' => 'Img',
'link' => 'Link',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getShops()
{
return $this->hasMany(Shop::className(), ['id_shop_value' => 'id_shop_value'])->inverseOf('idShopValue');
}
public function getShopCategors()
{
$this->hasMany(ShopCategor::className(), ['id_categor' => 'id_shop_categor'])->viaTable('shop', ['id_shop_value' => 'id_shop_value']);
}
public function behaviors()
{
return
[
[
'class' => \voskobovich\behaviors\ManyToManyBehavior::className(),
'relations' => [
'shop_categor_list' => ['shop_categors'],
]
]
];
}
public static function find()
{
return new ShopvalueQuery(get_called_class());
}
}
<div class="shopvalue">
<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>
<?= $form->field($model, 'val')->textInput() ?>
<?= $form->field($model, 'id_shop_categor')->textInput() ?> -вот здесь ошибка
<?= $form->field($model, 'img')->widget(\kartik\file\FileInput::classname(),[
'options'=>['accept'=>'image/*'],
'pluginOptions'=>[
//'uploadUrl'=>\Yii\helpers\Url::to(['file-image']),
//'ulpoadExtraData'=>['id'=>1],
'allowedFileExtensions'=>['jpg','png'],
// 'initialPreview'=>$image,
'ShowUpload'=>false,
'ShowRemove'=>false,
'dropZoneEnabled'=>true
]
]); ?>
<div class="form-group">
<?= Html::submitButton ($model->isNewRecord ? 'Добавить' : 'Изменить',[ 'class'=> 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Answer the question
In order to leave comments, you need to log in
You can use events in the model, namely beforesave and aftersave or www.yiiframework.com/doc-2.0/guide-db-active-recor...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question