某物';我的cakePHP保存方法有什么问题
我在模型中有一个方法,可以执行简单的保存(插入到数据库),首先它会查找满足特定条件的任何用户,然后将特定信息插入到另一个表中,并将这些信息插入到每个找到的用户,代码如下:某物';我的cakePHP保存方法有什么问题,php,cakephp,Php,Cakephp,我在模型中有一个方法,可以执行简单的保存(插入到数据库),首先它会查找满足特定条件的任何用户,然后将特定信息插入到另一个表中,并将这些信息插入到每个找到的用户,代码如下: function save_emails(){ App::import('Model', 'User'); App::import('Model', 'EmailSave'); $this->EmailSave = new EmailSave(); $this->User
function save_emails(){
App::import('Model', 'User');
App::import('Model', 'EmailSave');
$this->EmailSave = new EmailSave();
$this->User = new User();
$users = $this->User->find("all", array(
'conditions' => array(
'OR' => array(
array('User.user_type_id' => '4'),
array('User.user_type_id' => '1')
)
),
'fields' => array(
'username',
'email'
),
'recursive' => -1
));
foreach($users as $user){
//pr($user);
$to = $user['User']['email'];
$subject = $email_template['subject'];
$template = //calling a templating method;
$save_send['EmailSave']['created'] = date("Y-m-d H:i:s", time());
$save_send['EmailSave']['modified'] = date("Y-m-d H:i:s", time());
$save_send['EmailSave']['send_to'] = $to;
$save_send['EmailSave']['subject'] = $subject;
$save_send['EmailSave']['message'] = $template;
//pr($save_send);
$this->EmailSave->save($save_send);
//echo $to."<br />".$subject."<br />".$template."<br />".$cron_id."<br />";
}
}
它返回2个结果,当我尝试使用cake的pr方法打印结果时,它将打印正确的结果,但是…如果我应用save方法,它将只插入一个结果,这怎么可能?我一行一行地跟踪代码,它刚刚找到,问题是当我将它们保存到数据库时,它无法保存所有2个结果,请帮助。
创建或更新由模型的id字段控制。如果设置了$Model->id
,则会更新具有此主键的记录。否则将创建一个新记录
在循环中调用save()
时,不要忘记调用create()
$users = $this->User->find("all", array(
'conditions' => array(
'OR' => array(
array('User.user_type_id' => '4'),
array('User.user_type_id' => '1')
)
),
'fields' => array(
'username',
'email'
),
'recursive' => -1
));