Return to Snippet

Revision: 10733
at January 11, 2009 17:50 by gerhardsletten


Initial Code
function addedit($id = null) {
	if (empty($this->data)) {
		if($id) {
			// Edit data, populate fields
			$this->data = $this->Model->read(null, $id);			
		} else {
			// Create, prefill some fields..
			$the_user = $this->Auth->user();
			$this->data['Model']['user_id'] = $the_user['User']['id'];
		}
	} else {
		if($id == null) {
			// Create model
			$this->Model->create();
		}
		if ($this->Article->save($this->data)) {
			$this->Session->setFlash(__('The Article has been saved', true));
			
			// Add this to form: 
			// echo $form->submit(__('Save and close', true), array('name'=>'close'));
			if(isset($this->params['form']['close'])) {
				$this->redirect(array('action'=>'index'));
			}
			if($id == null) {
				$new_id = $this->Model->getInsertID();
				$this->redirect(array('action'=>'addedit', $new_id));
			}
			$this->data = $this->Model->read(null, $id);	
		} else {
			$this->Session->setFlash(__('The Model could not be saved. Please, try again.', true));
		}
	}
}

Initial URL


Initial Description


Initial Title
Cakephp addedit function

Initial Tags
textmate, cakephp

Initial Language
PHP