CodeIgniter Forums
Model library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: Model library (/thread-23244.html)

Pages: 1 2


Model library - El Forum - 10-04-2009

[eluser]Unknown[/eluser]
I have made a model library for codeigniter. It is based on the article http://maestric.com/doc/php/codeigniter_models

The code is on my blog
http://csotelo.blogspot.com/2009/10/model-library-for-codeigniter-php-if.html

Please check and test it
regards


Model library - El Forum - 10-05-2009

[eluser]eoinmcg[/eluser]
Thanks for this Carlos. I'll take a look and let you know if I encounter any problems.

You may also be interested in this:
http://www.phpfour.com/blog/2008/07/extended-model-for-codeigniter/

Which was apparently inspired by cakephp's Model implementation


Model library - El Forum - 10-05-2009

[eluser]imn.codeartist[/eluser]
nice, keep up the good work :-)


Model library - El Forum - 10-05-2009

[eluser]davidbehler[/eluser]
What happens if my primary key is not 'id' but 'table_id' or something similar? Wink

I think models should be more flexible and not be restricted to a specific set of naming conventions Smile


Model library - El Forum - 10-05-2009

[eluser]ray73864[/eluser]
i have to agree with waldmeister on this one.


Model library - El Forum - 10-05-2009

[eluser]imn.codeartist[/eluser]
[quote author="waldmeister" date="1254746929"]What happens if my primary key is not 'id' but 'table_id' or something similar? Wink

I think models should be more flexible and not be restricted to a specific set of naming conventions Smile[/quote]

In that case, there you needs to write another wrapper class where you can run the query "desc tablename" and get the primary key and take the value as primary key


Model library - El Forum - 10-05-2009

[eluser]davidbehler[/eluser]
Wouldn't it be easier to adjust your model slightly? Wink
E.g. by adding another attribute:
Code:
class MY_Model extends Model {

    var $table = "";
    var $primary_key = "id";
and using that attribute in your methods instead of 'id'?


Model library - El Forum - 10-05-2009

[eluser]hugle[/eluser]
[quote author="waldmeister" date="1254749400"]Wouldn't it be easier to adjust your model slightly? Wink
E.g. by adding another attribute:
Code:
class MY_Model extends Model {

    var $table = "";
    var $primary_key = "id";
and using that attribute in your methods instead of 'id'?[/quote]

good point waldmeister Smile)

your examples are everywhere Smile

I just wanted to ask, how do I pass the $table value to that model?

If I do for example ....

model:
Code:
class Book extends MY_Model {

    function Book()
    {
        parent::MY_Model();
    }

and the controller:

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

$book = array();
$book['title'] = 'The Man';
$book['author'] = 'Unknown';

$this->book->insert($book);

But as I said, I need to specify $table and por example $primary_key in model, so it updates/inserts/finds in different tables...

how is that done? I think that should be done in Model...

thanks !


Model library - El Forum - 10-05-2009

[eluser]davidbehler[/eluser]
Following the example in the original blog post:
Code:
class Book extends MY_Model {

    function Book()
    {
        parent::MY_Model();
        $this->table = 'book';
        $this->primary_key = 'book_id';
    }



Model library - El Forum - 10-05-2009

[eluser]ray73864[/eluser]
have the runtime configs in the model itself, , then you should be able to do things like:

Code:
class Book extends MY_Model {
    var $table = "";
    var $primary_key = "id";

    function Book()
    {
      parent::MY_Model();
    }

    function select_by_pk($key_value)
    {
      select * from $this->table where $this->primary_key = $key_value;
    }
  }

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

  $this->book->table = 'my_table';
  $this->book->primary_key = 'id';