Welcome Guest, Not a member yet? Register   Sign In
Model library
#1

[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
#2

[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
#3

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

[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
#5

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

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

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

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB