Welcome Guest, Not a member yet? Register   Sign In
is this legal in a model?
#1

[eluser]speedskater[/eluser]
Hi guys, i have a question about functionality in a model
this is my model

Code:
class Blogmodel extends Model {

    function Blogmodel(){
        parent::Model();
    }
    
    
    function get_all_categories()
    {
        $this->db->select('id, name, description, position');
        $query = $this->db->get('categories');
        if($query->num_rows > 0){
            return $query->result_array();
        } else {
            return $query = array(array('name' => 'Nog geen categorieën toegevoegd', 'description' => 'Geen resultaat gevonden op je query'));
        }
    }
}
So in fact what i do here is delivering a message when the query doesn't have results. No is my question, can you do that in a model, or should you do that in the controller?

Thanx in advance
#2

[eluser]xwero[/eluser]
If you return an array for that method why not returning an empty array.
Code:
function get_all_categories()
    {
        $this->db->select('id, name, description, position');
        $query = $this->db->get('categories');

        return ($query->num_rows > 0)? $query->result_array():array();
    }
For your question is it wrong, the short answer is no. I think it's even better than doing
Code:
function get_all_categories()
    {
        $this->db->select('id, name, description, position');
        $query = $this->db->get('categories');

        return ($query->num_rows > 0)? $query->result_array():false;
    }
Because then you haven't a consistent output.

It's up to the developer if he wants to do the first data manipulation in the model or in the controller.
#3

[eluser]Michael Wales[/eluser]
You can do it in the model if it works for you.

Personally, I like to easily know what's happening in my controller/view. Using this method it's a bit more difficult to do that - you just echo out whatever the mode returns in your view.

I usually do something like this, so I know whether the model's method was successful or failed:

Model
Code:
function get_all_categories()
    {
        $this->db->select('id, name, description, position');
        $query = $this->db->get('categories');
        if($query->num_rows > 0){
            return $query->result();
        } else {
            return FALSE;
        }

Controller:
Code:
function whatever() {
  $this->data->categories = $this->blogmodel->get_all_categories();
  $this->load->view('view', $this->data);

View:
Code:
if ($categories) {
  // Do Stuff
} else {
  // My model method failed... do something else
}

It leads to a bit more typing but you also have a bit more control. I guess you could do something similar by checking to see if the name variable was your error message
Code:
if ($categories->name = 'Nog geen categorieën toegevoegd') {
  // My method failed
}
#4

[eluser]xwero[/eluser]
So i have another view than Michael. In the view using an array as return you do
Code:
if(count($categories) == 0)
{
   // no data
}
else
{
  // output the data
}

One small error in Michaels code $categories->name needs to be changed to $categories[0]['name'] if you use your array. As far as i know you can't use the object syntax for arrays.
#5

[eluser]speedskater[/eluser]
Ah thanx, those are some nice ideas, that helped me out great!

Super.
#6

[eluser]Michael Wales[/eluser]
Thanks for pointing that out xwero - I noticed he used result_array() after the post but didn't feel the need to go back and change - although now I see it appears as if I missed a close bold tag....

Edit: Closed the bold tag and changed it to the result() method - haha! Objects FTW!




Theme © iAndrew 2016 - Forum software by © MyBB