CodeIgniter Forums

Full Version: Model library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[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/mode...hp-if.html

Please check and test it
regards

El Forum

[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/exte...deigniter/

Which was apparently inspired by cakephp's Model implementation

El Forum

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

El Forum

[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

El Forum

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

El Forum

[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

El Forum

[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'?

El Forum

[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 !

El Forum

[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';
    }

El Forum

[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';
Pages: 1 2