MY_MODEL

#1
[eluser]the_unforgiven[/eluser]
Can anyone explain how the lib 'MY_MODEL' works that's floating around the forums, net?

I want a CRUD script i can use all the time without repeating myself, is this what it is?

#2
[eluser]PhilTem[/eluser]
Yes, that's what it is. But it's just for database crudding, not actual controller crudding liek GroceryCRUD.

For some nice MY_Models look at
https://github.com/jamierumbelow/codeigniter-base-model
or
https://github.com/ignitedcode/iCF/blob/..._Model.php

#3
[eluser]the_unforgiven[/eluser]
This https://github.com/jamierumbelow/codeigniter-base-model is the one I have, so my understanding is that;

I can use
Code:
class Test extends My_Model {
      public function myMethod {
           // Then be able to use like $this->db->get(); as per jamies My_Model is that correct?
           // does it not use the core CI_Model/$this->db->get() as per the manual?
      }
}

#4
[eluser]johnpeace[/eluser]
No, it would actually be:

Code:
class Things extends MY_Model {
    // automatically set to plural form of your model name...in this case, 'thingss'

    var $_table = 'things';

    public function __construct() {
        parent::__construct();
    }

}

Then, to use it:

Code:
$this->load->model('things');

$this->things->get_all();

#5
[eluser]the_unforgiven[/eluser]
Gracias (Thanks) your a legend just what I needed thank you!

#6
[eluser]TunaMaxx[/eluser]
For Jamie's MY_Model, you are supposed to name your model with the singular version of the table name, and the table name should be plural. That way, you generally don't have to explicitly set the $_table property.

Also, there is no need to call parent::__construct or even have a __construct() method at all in most cases. So your entire 'Things' model could be just a one liner if the MY_Model methods cover your needs:

Code:
<?php class Thing extends MY_Model {}

...and then to use it:

Code:
$this->load->model('thing');
$this->thing->get_all();


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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