I’m using the following code to attempt to mass compose a message (insert multiple records into the message table that comes with Yii by default). The following code throws no error, yet it doesn’t insert any records. I’ve tried it both with the loop below and then just by attempting to manually insert a single record. Neither works. What am I missing?
Attempt to insert multiples from loop:
try {
$attr = array();
foreach($msgUsers as $id) {
$a = array(
'message'=>$model->message,
'title'=>$model->title,
'from_user_id'=>Yii::app()->user->id,
'to_user_id'=>$id,
'timestamp'=>time(),
'message_read'=>0,
'answered'=>0
);
$attr[] = $a;
}
$builder = Yii::app()->db->schema->commandBuilder;
$command = $builder->createMultipleInsertCommand('message', $attr);
print_r($command->execute());
Yii::app()->end();
} catch (Exception $e) {
print_r($e->getMessage());
Yii::app()->end();
}
Attempt to insert a single record manually:
try {
$attr = array();
foreach($msgUsers as $id) {
$a = array(
'message'=>$model->message,
'title'=>$model->title,
'from_user_id'=>Yii::app()->user->id,
'to_user_id'=>$id,
'timestamp'=>time(),
'message_read'=>0,
'answered'=>0
);
$attr[] = $a;
}
$builder = Yii::app()->db->schema->commandBuilder;
$command = $builder->createMultipleInsertCommand('message', array('message'=>'sasdasds', 'title'=>'wdwd',
'from_user_id'=>16, 'to_user_id'=>1007, 'timestamp'=>1455507414, 'message_read'=>0, 'answered'=>0));
print_r($command->execute());
Yii::app()->end();
} catch (Exception $e) {
print_r($e->getMessage());
Yii::app()->end();
}