Custom Find method in Model

edited February 2013 in Bug Reports
Not specifically a bug, but a general 'how do I do this' question, not related to Modules.

I have the need for my model to return to me a result set based on a WHERE NOT IN () clause. And I'd like results to be of my defined record class (extending Base_model_record)

The find_all() methods take a 'where' but do not address the inverse. I can generate the result set that I want with the following function , which uses the CI active record methods directly.

class Teammembers_model extends Base_module_model { ... function find_directors() { // use straight CI query to take advantage of the 'not in' SQL, which // apparently isn't available to find_all $display_order = array('Chair', 'Vice-Chair', 'Treasurer', 'Secretary'); parent::_common_query(); // to do active and published $this->db->where_not_in('role_position', $display_order); $Q = $this->db->get('teammembers'); log_message("INFO", $this->db->last_query()); return $Q->result(); }

And in my view I can use it thusly:

$directors = fuel_model('teammembers', array('find' => 'directors'));

But, this returns a standard array .... and now I don't have any of my custom record methods, nor do they inherit the base_model_record methods.

P.S. A Category of 'general help' wold be good for something like this I think.

Comments

  • edited 5:58AM
    Use $this->get() instead of $this->db->get. $this->get will automatically wrap the returned results with your record class.
    http://www.getfuelcms.com/user_guide/libraries/my_model/table_class_functions
  • edited 5:58AM
    Thanks for the quick reply. Can't believe I missed that.

    In the mean time, I recalled that CI where() method permits operators in the key of the array ... so I was able to use the following

    $where = array('role_position NOT ' => $display_order); return $this->find_all($where);

    which gave me 'WHERE role_postion NOT IN ()"

    Different approaches to the same end. I think this is a little more FUEL-like.
Sign In or Register to comment.