How to write data from one controller to a different table in yii2?

Hi, there are two tables
post[id, title, text]
site[id, title, link, post_id]

When creating a post there are two fields for adding the site, with text and link are displayed with extensions https://github.com/unclead/yii2-multiple-input

in the model Post.php create a variable public $site and validate checks what is an array

[['site'], 'exist', 'allowArray' => true],


in VISCA _form make the field

<?= $form->field($model, 'site')->widget(MultipleInput::className(), [

 'columns' => [
[
 'name' => 'title',
 'title' => 'site Name',
 'enableError' => true,
],

[
 'name' => 'link',
 'title' => 'Link',
 'enableError' => true,
]
]
]);
?>


in the controller

public function actionCreate()
{
 $model = new Offers();
 if ($model->load(Yii::$app->request->post())) {
 if($model->save()) {
 foreach ($model->site as $var){
 $site = new Site(); 
 $site->title = $var->title;
 $site->link = $var->link;
 $site>post_id = $model->id;
 $site->save(); 

}

}

 return $this->redirect(['view', 'id' => $model->id]);

}

 return $this->render('create', [
 'model' => $model,

]);
 }


Trying to record produces an error at this place if($model->save()) {

Database Exception – yii\db\Exception
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[[]]) FROM `post` WHERE `post`.`site`=NULL,' at line 1
The SQL being executed was: SELECT COUNT(DISTINCT [[]]) FROM `post` WHERE `post`.`site`=NULL

Error Info: Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[[]]) FROM `post` WHERE `post`.`site`=NULL,' at line 1
)


What's wrong?
March 12th 20 at 08:05
1 answer
March 12th 20 at 08:07
Try this:
$model->save(false);
Instead of if($model->save())
Is it all right to ignore errors? to work will affect? - Erick27 commented on March 12th 20 at 08:10
@Erick27, Test, maybe you have a similar problem? https://yiiframework.ru/forum/viewtopic.php?t=7592 - murphy.Hilll commented on March 12th 20 at 08:13

Find more questions by tags YiiMySQL