Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>the easiest way i've found to deal with this is to always <strong>check for the negative condition first with an if check</strong>. this also makes it easy to check multiple steps. </p> <p>always return something back from your model methods whenever possible. Like even in this example - we need to validate the form - i would use codeigniters form validation to validate that its an integer etc. if it passes validation then we need the $personId for the database search. So instead of just returning true/false from the validation - if validation passes then return the $personId : </p> <pre><code>function getperson() { // Validate the form // AND if its valid, return the validated $personId // Note the separate private method if the validation failed if ( ! $personId = $this-&gt;person-&gt;_validateGetPersonForm() ) { $this-&gt;error_msg = 'Error in validating the form. Please use a number.'; $this-&gt;_showGetPersonFailed() ; } elseif ( ! $person = $this-&gt;person-&gt;getBy($personId) ) { $this-&gt;error_msg = 'There is no person in our records with the number:'. $personId; $this-&gt;_showGetPersonFailed() ; } else { $this-&gt;_showResultsFor($person) ; } } </code></pre> <p>$this->error_msg is automatically available for any of your views and because we have broken out validation and the database search - the error message is easily appropriate for the exact condition. if the search failed then there is a private method _showGetPersonFailed() to show the form again. the last if / else is usually success - there is a separate private method to handle showing the results with the appropriate views. </p> <p>also suggest not using the word "model" in the file name like "person_model". it just clutters up the overall naming and forces you to type the word model over and over again :-) think of it this way: if you are calling something from a controller and getting back results - its almost always going to be a model. and all your model files will always be in a folder named models. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload