Cakephp addedit function


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function addedit($id = null) {
  2. if (empty($this->data)) {
  3. if($id) {
  4. // Edit data, populate fields
  5. $this->data = $this->Model->read(null, $id);
  6. } else {
  7. // Create, prefill some fields..
  8. $the_user = $this->Auth->user();
  9. $this->data['Model']['user_id'] = $the_user['User']['id'];
  10. }
  11. } else {
  12. if($id == null) {
  13. // Create model
  14. $this->Model->create();
  15. }
  16. if ($this->Article->save($this->data)) {
  17. $this->Session->setFlash(__('The Article has been saved', true));
  18.  
  19. // Add this to form:
  20. // echo $form->submit(__('Save and close', true), array('name'=>'close'));
  21. if(isset($this->params['form']['close'])) {
  22. $this->redirect(array('action'=>'index'));
  23. }
  24. if($id == null) {
  25. $new_id = $this->Model->getInsertID();
  26. $this->redirect(array('action'=>'addedit', $new_id));
  27. }
  28. $this->data = $this->Model->read(null, $id);
  29. } else {
  30. $this->Session->setFlash(__('The Model could not be saved. Please, try again.', true));
  31. }
  32. }
  33. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.