Answer the question
In order to leave comments, you need to log in
Why doesn't my tags save?
How to get the saving of tags, it seems that the system should save an array of tags, but for some reason it does not save.
My implementation Model tag.php
namespace app\models;
class Tag extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'tag';
}
...
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Наименование',
];
}
}
namespace app\models;
use Yii;
use yii\helpers\ArrayHelper;
use yii\db\ActiveRecord;
class Zakaz extends ActiveRecord
{
...
public $tags_array;
public static function tableName()
{
return 'zakaz';
}
public function rules()
{
return [
...
[['tags_array'], 'safe'],
...
];
}
public function attributeLabels()
{
return [
'id_zakaz' => '№',
...
'tags_array' => 'Тэги',
];
}
public function getZakazTag()
{
return $this->hasMany(ZakazTag::className(), ['zakaz_id' => 'id_zakaz']);
}
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->via('zakazTag');
}
public function afterFind()
{
return $this->tags_array = $this->tags;
}
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
$arr = ArrayHelper::map($this->tags, 'id', 'id');
foreach ($this->tags_array as $one){
if (!in_array($one, $arr)){
$model = new ZakazTag();
$model->zakaz_id = $this->id_zakaz;
$model->tag_id = $one;
$model->save();
}
}
}
}
namespace app\models;
use Yii;
class ZakazTag extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'zakaz_tag';
}
public function rules()
{
return [
[['zakaz_id', 'tag_id'], 'required'],
[['zakaz_id', 'tag_id'], 'integer'],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'zakaz_id' => 'Zakaz ID',
'tag_id' => 'Tag ID',
];
}
public function getTag()
{
return $this->hasOne(Tag::className(), ['id' => 'tag_id']);
}
}
<?= $form->field($model, 'tags_array')->widget(Select2::className(), [
'data' => ArrayHelper::map(Tag::find()->all(), 'id', 'name'),
'language' => 'ru',
'options' => ['placeholder' => 'Выберите тэг', 'multiple' => true],
'pluginOptions' => [
'allowClear' => true,
'tags' => true,
'maximumInputLength' => 10,
],
]);
?>
Answer the question
In order to leave comments, you need to log in
Maxim Timofeev I decided to leave afterSave, so as not to confuse me, and went according to the old proven scheme, through the controller.
Having surfed the Internet how to receive such requests
Zakaz => [
'tags_array' => [
0 => '3'
1 => '1'
2 => '2'
]
]
came across this post Yii::$app->request ->post('Zakaz')['tags_array'] and everything went very well, everything saves and deletes perfectly, you don’t even need to suffer to figure out why it doesn’t save in afterSave and why this particular array doesn’t process, maybe not enough experience or just not there is enough mentor, but everything works fine and for my perception it is more clear what comes from.
Now the controller is constantly cumbersome, because not only one model is saved there, but already 3 models are saved and it turns out that create and update are constantly becoming large.
But here is my code in the controller to save tags
if ($model->load(Yii::$app->request->post()) && $client->load(Yii::$app->request->post())) {
$arr = ArrayHelper::map($model->tags, 'id', 'id');
foreach (Yii::$app->request->post('Zakaz')['tags_array'] as $one){
if (!in_array($one, $arr)){
$tag = new ZakazTag();
$tag->zakaz_id = $id;
$tag->tag_id = $one;
$tag->save();
}
if (isset($arr[$one])){
unset($arr[$one]);
}
}
ZakazTag::deleteAll(['tag_id' => $arr]);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question