Welcome Guest, Not a member yet? Register   Sign In
General model questions
#1

[eluser]jimps[/eluser]
Hi,

I'm just wondering how you people returns information to the controller from the model when dealing with methods that can return many kinds of "errors".

For example, let's say that I'm building an account model. I have one method for registration and in this I'm validating all the POST-data and inserting it to the database. On success I return TRUE and on failure I return a json encoded array (for the AJAX).

So my question is if this is "right" or could i do it some way better?

Sorry for my damn poor english.

/Jimps
#2

[eluser]Buso[/eluser]
On failure I always return FALSE, and report the error in 2 different ways:

If the model method is directly related to the output, like when a user tries to login() and makes a mistake like writting a wrong password, I use a method I added to the base controller: add_errors(). It saves the error in a errors array, and then I fetch them with another method: errors(), which adds html tags to them, like <p='error'>one of the errors</p>.

In any other case I just save the errors to the error array of the object which detected the error (in our case, the model object), just in case I need them for debugging or something.
#3

[eluser]jimps[/eluser]
[quote author="Buso" date="1277777149"]On failure I always return FALSE, and report the error in 2 different ways:

If the model method is directly related to the output, like when a user tries to login() and makes a mistake like writting a wrong password, I use a method I added to the base controller: add_errors(). It saves the error in a errors array, and then I fetch them with another method: errors(), which adds html tags to them, like <p='error'>one of the errors</p>.

In any other case I just save the errors to the error array of the object which detected the error (in our case, the model object), just in case I need them for debugging or something.[/quote]

Oh, that's quite smart! But how do you communicate with the base controller from within the model?

Best regards,
Jimps
#4

[eluser]Buso[/eluser]
you can communicate with the codeigniter object (the controller you are using, not the base controller) with get_instance()

Code:
function some_model_method() {

  // something went wrong

  $CI =& get_instance();
  $CI->add_errors('Wrong Password!');

  return FALSE;
}

I usually save that reference in $this->CI inside __construct(), as I'm always using the CI object. If it's just one call you can do it like this: get_instance()->some_method();
#5

[eluser]jimps[/eluser]
[quote author="Buso" date="1277778441"]you can communicate with the codeigniter object with get_instance()

Code:
function some_model_method() {

  // something went wrong

  $CI =& get_instance();
  $CI->add_errors('Wrong Password!');

  return FALSE;
}

I usually save that reference in $this->CI inside __construct(), as I'm always using the CI object. If it's just one call you can do it like this: get_instance()->some_method();[/quote]

Ah, now i understand! That's pretty good, thank you. Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB