Php Laravel雄辩的ORM-如何在一个函数中保存多个实例
我有一个简单的私人消息程序,可以向其他用户发送消息。他们可以将信息发送给不止一个人,就像普通电子邮件一样。但是,当我在收件人上循环以向每个收件人的数据库中添加邮件时,它只保存其中一个收件人 是否需要为每个循环创建消息模型的新实例 MessagesController:Php Laravel雄辩的ORM-如何在一个函数中保存多个实例,php,laravel,laravel-4,eloquent,Php,Laravel,Laravel 4,Eloquent,我有一个简单的私人消息程序,可以向其他用户发送消息。他们可以将信息发送给不止一个人,就像普通电子邮件一样。但是,当我在收件人上循环以向每个收件人的数据库中添加邮件时,它只保存其中一个收件人 是否需要为每个循环创建消息模型的新实例 MessagesController: public function store() { // Validate the message data if( ! $this->isValid(Input::all(), $this->mes
public function store() {
// Validate the message data
if( ! $this->isValid(Input::all(), $this->message))
return Redirect::back()->withInput()->withErrors($this->message->inputErrors);
if( ! $this->message->sendMessage(Input::all())) {
//log error to logger
$errorNum = $this->logger->createLog('MessagesController', 'store', 'Failed to add message to DB.', Request::url(), Request::path(), 8);
Session::put('adminDangerAlert', 'Error #'. $errorNum . ' - Something went wrong attempting to save the message to the database. Contact an administrator if this continues.');
return Redirect::back()->withInput();
}
Session::put('adminSuccessAlert', 'Message sent.');
return Redirect::to('admin/messages');
}
消息模型:
public function sendMessage($input) {
$string = App::make('StringClass');
for($i = 0; $i < count($input['recipients']); $i++) {
//save new message to DB
$this->sender = intval(Auth::id());
$this->recipient = intval($input['recipients'][$i]);
$this->subject = $string->nullifyAndStripTags($input['subject']);
$this->body = $string->nullifyAndStripTags($input['body'], '<b><p><br><a><h1><h2><h3><h4><h5><h6><i><blockquote><u><ul><ol><li>');
$this->save();
}
return true;
}
公共函数sendMessage($input){
$string=App::make('StringClass');
对于($i=0;$isender=intval(Auth::id());
$this->recipient=intval($input['recipients'][$i]);
$this->subject=$string->nullifyAndStripTags($input['subject']);
$this->body=$string->nullifyAndStripTags($input['body'],“
- ”);
$this->save();
}
返回true;
}
您的Message@sendMessage
方法递归更新相同的精确模型,而不是创建新模型
如前所述,这最好在控制器中完成。您没有提供在控制器中执行此操作的尝试,但同样的问题可能也发生在控制器中
以下是您的MessagesController@store
方法应如下所示(未经测试):
公共函数存储(){
$input=input:all();
//验证消息数据
如果(!$this->isValid($input,$this->message))
return Redirect::back()->withInput()->withErrors($this->message->inputErrors);
$string=App::make('StringClass');
对于($i=0;$imessage->create(数组)(
“发件人”=>intval(Auth::id()),
“收件人”=>intval($input['recipients'][$i]),
'subject'=>$string->nullifyAndStripTags($input['subject']),
'body'=>$string->nullifizandstriptags($input['body'],“
- ”)
));
//现在,您可以在此迭代中访问新创建的消息
//通过$message。
//如果未创建,$message->exists将为false。
}
会话::put('adminSuccessAlert','MessageSent');
返回重定向::到('admin/messages');
}
现在我们使用Message@create
而不仅仅是更新同一型号的属性
我没有加入将返回
的错误检查,无法将消息添加到数据库。
因为Message@sendMessage
始终返回true,但您可以相当轻松地合并一个事务,以确保所有消息发送或不发送消息。理想情况下,递归应该发生在控制器中。@AliGajani也尝试过。仍然只向数据库添加1条消息。
public function store() {
$input = Input:all();
// Validate the message data
if( ! $this->isValid($input, $this->message))
return Redirect::back()->withInput()->withErrors($this->message->inputErrors);
$string = App::make('StringClass');
for($i = 0; $i < count($input['recipients']); $i++) {
// save new message to DB
$message = $this->message->create(array(
'sender' => intval(Auth::id()),
'recipient' => intval($input['recipients'][$i]),
'subject' => $string->nullifyAndStripTags($input['subject']),
'body' => $string->nullifyAndStripTags($input['body'], '<b><p><br><a><h1><h2><h3><h4><h5><h6><i><blockquote><u><ul><ol><li>')
));
// You can now access the newly created message in this iteration
// via $message.
// If it wasn't created, $message->exists will be false.
}
Session::put('adminSuccessAlert', 'Message sent.');
return Redirect::to('admin/messages');
}