i have method inside model, simple save (insert database), first finds users meet conditions, , insert info table each of found users, here's code:
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 $user){ //pr($user); $to = $user['user']['email']; $subject = $email_template['subject']; $template = //calling 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 />"; } }
this pretty straight forward, mean not using complicated methods, when run find method towards 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 ));
it returns 2 results, , when try print result using cake's pr method, print right results, but...if apply save method, insert 1 of result, how possible? have traced codes line line , went through find, problem when save them database, fails save 2 results, please help.
http://book.cakephp.org/view/1031/saving-your-data
creating or updating controlled model's id field. if
$model->id
set, record primary key updated. otherwise new record created.when calling
save()
in loop, don't forget callcreate()
.
Comments
Post a Comment