ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

MY_Model Base CRUD Model

November 08, 2009 9:02am

Subscribe [19]
  • #31 / Jan 17, 2010 10:20pm

    megabyte

    179 posts

    I would change this:

    public function get_many_by() {
            $where =& func_get_args();
            $this->_set_where($where);
            
            return $this->get_all();
        }


    to this though:

    public function get_many_by() {
            $where =& func_get_args();
            $this->_set_where($where);
            
            return $this->db->get($this->_table)
                ->result();
        }
  • #32 / Jan 23, 2010 8:36pm

    darkhouse

    323 posts

    First, great work.  This will definitely save me some time.

    What’s the best practice when you need to use order_by?  Do you just create a model function like you would normally, and then use $this->get_many_by() or $this->get_all()?  Seems like it should be part of the base model, but I’m not really sure what the best way to handle it would be considering you’re using func_get_args to manage the where data.

    Edit: I just realized there’s an order_by method, so I’m guessing the best practice is to do:

    $this->some_model->order_by('title');
    $stuff = $this->some_model->get_all();

    Also, maybe you have your reasons for doing it the way you did it, but I think you could change the insert_many function from this:

    public function insert_many($data) {
            $ids = array();
            
            foreach ($data as $row) {
                $data = $this->_run_before_create($row);
                    $this->db->insert($this->table, $row);
                $this->_run_after_create($row, $this->db->insert_id());
        
                $ids[] = $this->db->insert_id();
            }
            
            return $ids;
        }

    to this:

    public function insert_many($data) {
            $ids = array();
            
            foreach ($data as $row) {
                $ids[] = $this->insert($row);
            }
            
            return $ids;
        }

    Just gets rid of some redundancy.  I’m a fan of DRY, I noticed you did that for get_many_by using get_all, so I figured why not this function.

  • #33 / Mar 06, 2010 11:20am

    rkjaer

    5 posts

    Anyone managed to make this support multiple databases? I need this solution for my current project, but had no luck yet. Unfortunately the author Jamie does not have time for this suggested feature :(

  • #34 / Mar 09, 2010 7:43am

    Phil Sturgeon

    2889 posts

    Darkhouse: I added some method chaining to order_by() and limit() so you can do this:

    $stuff = $this->some_model->order_by('title')->get_all();

    rkj: I’ll have a look at that tonight. It should be pretty easy to sort out.

  • #35 / Mar 09, 2010 7:46am

    darkhouse

    323 posts

    Awesome! Thanks!

  • #36 / Mar 09, 2010 7:59am

    rkjaer

    5 posts

    rkj: I’ll have a look at that tonight. It should be pretty easy to sort out.

    Thanks a bunch. Looking forward to it! 😊

  • #37 / Mar 09, 2010 10:30am

    ElToro

    16 posts

    This is nice CRUD, but it is licensed as GPL3v. Does it mean that if I use it on some project I need to make the hole project as open source?

  • #38 / Mar 09, 2010 10:46am

    Phil Sturgeon

    2889 posts

    I am going to assume not. It’s more likely Jamie didn’t fully think through the license. I made the mistake of using GPL on my projects, only to realize it is horrible.

  • #39 / Mar 23, 2010 11:17am

    pdev

    1 posts

    I am currently trying to program a simple function, which checks if several tags (array) is already in the database and if it’s not, it inserts the tags. I am trying to do an initial check by using the MY_Model before_create command to execute my “check_tag” function. I have tried different things but no luck so far.

    Any advice on how to do this from this little model snippet? Thanks in advance and a helpful model!

    function check_tag($tag) {
      $this->_table = 'tags';
      return parent::count_by('tag', $tag);
     }
     
     function insert_tags($tags) {
      $this->_table = 'tags';
      return parent::insert_many($tags);
     }
  • #40 / Apr 04, 2010 8:11am

    ciKD

    17 posts

    This is a very nice MY_Model, but can it handle join somehow? Or did I just miss the logic how to use it with joins?

  • #41 / Apr 04, 2010 8:24am

    rkjaer

    5 posts

    This is a very nice MY_Model, but can it handle join somehow? Or did I just miss the logic how to use it with joins?

    Yes you can in a fairly easy way 😊 Below is an example usin MY_Models “get_all” with a join query and ordering from CI’s active records. Note that it ends up with calling its parent (get_all).

    function get_all()
    {        
        $this->db->select('table1.*, a.something')
            ->join('table2 a', 'a.id=table1.something')
            ->where('somerow', '1');
            
        $this->db->order_by('id', 'desc');
            
        return parent::get_all();
    }
  • #42 / Apr 04, 2010 9:03am

    ciKD

    17 posts

    Thanks for quick reply!

    So this MY_Model itself does not support a special join param in its methods but I can add the join(s) by myself and let MY_Model get the data. You named the function function get_all(), but I can name it anything i like, right?

    function whatever_i_want(params)
    {        
        $this->db->select('table1.*, a.something')
            ->join('table2 a', 'a.id=table1.something')
    ...
        return parent::get_all();
    }
  • #43 / Apr 04, 2010 9:10am

    rkjaer

    5 posts

    Thanks for quick reply!

    So this MY_Model itself does not support a special join param in its methods but I can add the join(s) by myself and let MY_Model get the data. You named the function function get_all(), but I can name it anything i like, right?

    You’re welcome! Exactly. I see you got the idea already 😉 You can name them just as you like and call whatever parent you need 😊

  • #44 / Apr 04, 2010 9:21am

    darkhouse

    323 posts

    If you’re going to do that, you don’t need to call parent - not that it makes much difference.  You can do this:

    function whatever_i_want(params)
    {        
        $this->db->select('table1.*, a.something')
            ->join('table2 a', 'a.id=table1.something')
    ...
        return $this->get_all();
    }

    The result will be the same.  I only use parent:: when I’m overriding a method like rkjaer wrote.  Since you’re calling it whatever you want, you can use $this-> instead of parent::

  • #45 / Apr 04, 2010 9:28am

    rkjaer

    5 posts

    If you’re going to do that, you don’t need to call parent - not that it makes much difference.  You can do this:

    function whatever_i_want(params)
    {        
        $this->db->select('table1.*, a.something')
            ->join('table2 a', 'a.id=table1.something')
    ...
        return $this->get_all();
    }

    The result will be the same.  I only use parent:: when I’m overriding a method like rkjaer wrote.  Since you’re calling it whatever you want, you can use $this-> instead of parent::

    Oh, yes. Sorry - you’re so right about that. 😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases