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

#41
[eluser]rkjaer[/eluser]
[quote author="ciKD" date="1270397463"]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?[/quote]

Yes you can in a fairly easy way Smile Below is an example usin MY_Models "get_all" with a join query and ordering from CI's active records. Note that it ends up with calling its parent (get_all).

Code:
function get_all()
{        
    $this->db->select('table1.*, a.something')
        ->join('table2 a', 'a.id=table1.something')
        ->where('somerow', '1');
        
    $this->db->order_by('id', 'desc');
        
    return parent::get_all();
}

#42
[eluser]ciKD[/eluser]
Thanks for quick reply!

So this MY_Model itself does not support a special join param in its methods but I can add the join(s) by myself and let MY_Model get the data. You named the function function get_all(), but I can name it anything i like, right?

Code:
function whatever_i_want(params)
{        
    $this->db->select('table1.*, a.something')
        ->join('table2 a', 'a.id=table1.something')
...
    return parent::get_all();
}

#43
[eluser]rkjaer[/eluser]
[quote author="ciKD" date="1270400584"]Thanks for quick reply!

So this MY_Model itself does not support a special join param in its methods but I can add the join(s) by myself and let MY_Model get the data. You named the function function get_all(), but I can name it anything i like, right?[/quote]

You're welcome! Exactly. I see you got the idea already Wink You can name them just as you like and call whatever parent you need Smile

#44
[eluser]darkhouse[/eluser]
If you're going to do that, you don't need to call parent - not that it makes much difference. You can do this:
Code:
function whatever_i_want(params)
{        
    $this->db->select('table1.*, a.something')
        ->join('table2 a', 'a.id=table1.something')
...
    return $this->get_all();
}

The result will be the same. I only use parent:: when I'm overriding a method like rkjaer wrote. Since you're calling it whatever you want, you can use $this-> instead of parent::

#45
[eluser]rkjaer[/eluser]
[quote author="darkhouse" date="1270401669"]If you're going to do that, you don't need to call parent - not that it makes much difference. You can do this:
Code:
function whatever_i_want(params)
{        
    $this->db->select('table1.*, a.something')
        ->join('table2 a', 'a.id=table1.something')
...
    return $this->get_all();
}

The result will be the same. I only use parent:: when I'm overriding a method like rkjaer wrote. Since you're calling it whatever you want, you can use $this-> instead of parent::[/quote]

Oh, yes. Sorry - you're so right about that. Smile

#46
[eluser]Unknown[/eluser]
Hello.

I included this function on your model, it works for MYSQL version 5.x


It fetch the primary key, without have to change it, I call it on __construct


Code:
private function _fetch_pkey()
    {
        if ($this->table !== NULL)
        {
            $_table_data     = $this->db->query('SHOW KEYS FROM `'. $this->table .'`')->row();
            $this->primary_key    = $_table_data->Column_name;
        }
    }

#47
[eluser]ciKD[/eluser]
Looks interesting!

With a similar approach, could you write a version which fetches the column-names of the table to an array so a function to (auto)validate a given order_by field could be added, using an in_array() check?

#48
[eluser]ed_ball[/eluser]
All links in original post now seem to be broken - what's the deal?

#49
[eluser]michalsn[/eluser]
Check here: https://bitbucket.org/jamierumbelow/code...-model/src

#50
[eluser]ed_ball[/eluser]
Thanks. I've installed this now but having problems with the table property in MY_Model conflicting with the native CI table method. The workaround I am employing currently is to 'reset' $this->table to 'new CI_table' before setting table heading etc as otherwise the following error occurs "Call to a member function set_heading() on a non-object...". Could this be considered a bug in the code or am I suffering from a lack of understanding of OOP?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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