Google

Kategorie

Reprezentacja Wiedzy

Kalendarz

August 2007
M T W T F S S
« Jul   Sep »
 12345
6789101112
13141516171819
20212223242526
2728293031  

CakePHP 1.2 : zapisywanie modeli

August 23rd, 2007 by prond

Domyślnie ‘wypieczony’ w Cake 1.2 kod zapisujący model wygląda następująco:

function add() {
	if (!empty($this->data)) {
		$this->cleanUpFields();
		$this->Page->create();
		if ($this->Page->save($this->data)) {
			$this->Session->setFlash('The Page has been saved');
			$this->redirect(array('action'=>'index'), null, true);
		} else {
			$this->Session->setFlash('The Page could not be saved. Please, try again.');
		}
	}
}

Moim zdaniem brakuje w nim rozróżnienia sytuacji, w której pola są nie poprawne od błędu przy zapisie do bazy.

Można to naturalnie wprowadzić przez dodatkowy warunek, ale wtedy moim zdaniem za bardzo zagnieżdżamy IF’y - wygląda to po prostu brzydko / mniej klarownie.

function add()
{
	if (!empty($this->data)) {
		$this->cleanUpFields();
		$this->Page->create($this->data);
		if ($this->Page->validates()) {
			if ($this->Page->save()) {
				$this->Session->setFlash('The Page has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Page could not be saved. Please, try again.');
			}
		} else {
			$this->Session->setFlash('The Page does not validate.');
		}  
	}
  }

Wiadomo, że Cake pracuje również na PHP 4, ale skoro ogłoszno już śmierć czwórki to chyba spokojnie można już
pisać kod z wykorzystaniem wyjątków. Wystarczy spojrzeć jaką zyskujemy na czytelność :

function add() {
	if (!empty($this->data)) {
		try {
			$this->cleanUpFields();
			$this->Page->create($this->data);
			$this->Page->save();
			$this->Session->setFlash('The Page has been saved');
			$this->redirect(array('action'=>'index'), null, true);
		} catch (CakeModelValidatesException $e) {
			$this->Session->setFlash('The Page does not validate.');
		} catch (CakeModelSaveException $e) {
			$this->Session->setFlash('The Page could not be saved. Please, try again.');
		} catch (Exception $e) {
			$this->Session->setFlash('The Page could not be saved. Please, try again.');
		}
	}
}

Można to trochę skrócić skoro wyjątek CakeModelSaveException obsługujemy tak samo jak dowolny inny wyjątek:

function add() {
	if (!empty($this->data)) {
		try {
			$this->cleanUpFields();
			$this->Page->create($this->data);
			$this->Page->save();
			$this->Session->setFlash('The Page has been saved');
			$this->redirect(array('action'=>'index'), null, true);
		} catch (CakeModelValidatesException $e) {
			$this->Session->setFlash('The Page does not validate.');
		} catch (Exception $e) {
			$this->Session->setFlash('The Page could not be saved. Please, try again.');
		}
	}
}

Żeby korzystać z powyższej składni wystarczy zmodyfikować podstawową klasę modelu danej aplikacji (/{nazwa aplikacji}/app_model.php):

class CakeModelSaveException extends Exception {}
class CakeModelValidatesException extends Exception {}
 
class AppModel extends Model {
	function save($data = null, $validate = true, $fieldList = array()) {
		$save = parent::save($data,$validate,$fieldList);
		if(!$save) {
			Throw new CakeModelSaveException("{$this->name} could not be saved");
		}
 
		return $save;
	}
 
	function validates($data = array()) {
		$errors = parent::validates($data);
		if (!$errors) {
			Throw new CakeModelValidatesException("{$this->name} does not validate");
		}
 
		return $errors;
	}
}

Naturalnie definicje wyjątków lepiej wyciągnąc do venords.

Posted in CakePHP |

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.