Monday, November 19, 2012

Re: MVC

I personally like version 2 because then your model needn't declare a
generic $lastError var.

It's probably a typo but your model is MailAddress but your controller
is calling EmailAddress. Also, you might as well just name the model
method add().

On Mon, Nov 19, 2012 at 9:19 AM, Tobias Schlemmer
<tobias.schlemmer@gmail.com> wrote:
> Hi all,
>
> I have a question concerning the MVC logic. In my example I have to add a
> new user to a mail server via REST. And I would like to inform the user with
> the error message. So should the Model return the error message to the
> controller or should the controller request the error message from the
> model? What is best practice?
>
> 1) Model send message to controller
> Model:
> class MailAddress extends AppModel {
>
> public function addMailAddress( $mail, $password) {
> // create POST data
> $arrData = array('Password' => $password);
>
> // perform the request
> $result = $this->REST->request('POST', '/users/create/name/'.$mail,
> $arrData);
> // check the results
> if($result === false) {
> $this->lastError = $this->REST->lastError;
> return false;
> } elseif ( $result['api']['userCreate']['status'] == 'success' ) {
> return true;
> } else {
> $this->lastError =
> $result['api']['userCreate']['response']['message'];
> return false;
> }
> }
> }
>
>
> Controller:
> if($this->EmailAddress->addMailAddress($mail,$password)) {
> $this->Session->setFlash( __('Entry successfully saved.') );
> } else {
> $this->Session->setFlash( __('Error on adding: %s',
> $this->EmailAddress->lastError) );
> }
>
> 2) Controller gets message from the model
>
> Model:
> class MailAddress extends AppModel {
>
> public function addMailAddress( $mail, $password) {
> // create POST data
> $arrData = array('Password' => $password);
>
> // perform the request
> $result = $this->REST->request('POST', '/users/create/name/'.$mail,
> $arrData);
> // check the results
> if($result === false) {
> return new ErrorObject($this->REST->lastError);
> } elseif ( $result['api']['userCreate']['status'] == 'success' ) {
> return true;
> } else {
> return new
> ErrorObject($result['api']['userCreate']['response']['message']);
> }
> }
> }
> Controller:
> if(!($ret = $this->EmailAddress->addMailAddress($mail,$password)) instanceof
> ErrorObject) {
> $this->Session->setFlash( __('Entry successfully saved.') );
> } else {
> $this->Session->setFlash( __('Error on adding: %s', $ret->getMessage())
> );
> }
>
>
> 3) I don't want to use Exceptions here because i think returning values
> keeps the programming flow more straight.
>
> Thanks, Tobi
>
> --
> Like Us on FaceBook https://www.facebook.com/CakePHP
> Find us on Twitter http://twitter.com/CakePHP
>
> ---
> You received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com.
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php?hl=en.
>
>

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.

No comments: