• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MY_Model Base CRUD Model

#31
[eluser]megabyte[/eluser]
I would change this:

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


to this though:

Code:
public function get_many_by() {
        $where =& func_get_args();
        $this->_set_where($where);
        
        return $this->db->get($this->_table)
            ->result();
    }

#32
[eluser]darkhouse[/eluser]
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:
Code:
$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:
Code:
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:
Code:
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
[eluser]rkjaer[/eluser]
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 Sad

#34
[eluser]Phil Sturgeon[/eluser]
Darkhouse: I added some method chaining to order_by() and limit() so you can do this:

Code:
$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
[eluser]darkhouse[/eluser]
Awesome! Thanks!

#36
[eluser]rkjaer[/eluser]
[quote author="Phil Sturgeon" date="1268156633"]rkj: I'll have a look at that tonight. It should be pretty easy to sort out.[/quote]

Thanks a bunch. Looking forward to it! Smile

#37
[eluser]ElToro[/eluser]
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
[eluser]Phil Sturgeon[/eluser]
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
[eluser]Unknown[/eluser]
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!

Code:
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
[eluser]ciKD[/eluser]
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?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.