Welcome Guest, Not a member yet? Register   Sign In
Models, foreign keys and table associations - whats best practise in codeigniter?
#1

[eluser]warpkid[/eluser]
Hi There,
I am dipping my toe into php and codeigniter (coming from a .net background) and am begnning to attempt to build my first site.

Off the bat I am wondering the best way of handling model data in codeigniter. Let me expand...

In my test database I have two simple tables, organisations and buildings. A one to many relationship exists between them. Ideally, I would like my organisation model to be able to return an object containing organisation data, along with all related building rows.
Am i right in my understanding that there is no 'automagic' way for codeigniter to pick up on these relationships and return object graphs?

My model so far looks like this...
Code:
function GetOrganisationsAndBuildingDetails()
{
        $this->db->select('organisations.organisation_name,
                                           organisations.organisation_id,
                                           buildings.building_name,
                           buildings.address1');
        $this->db->from('organisations')->join('buildings', 'buildings.organisation_id = organisations.organisation_id');
        $query = $this->db->get();
        return $query->result();
}

This returns to my controller two organisation objects, each with separate building details, although the organisation details are the same (my database has one organisation in it with two related buildings).

Now, I can use a little code to build an array to pass to my view that contains the one org and its two buildings but id rather avoid this type of code as its going to get bulky if say, Im pulling data from 4 - 5 related tables (rare but it happens).

I suppose in short Im asking if there is a better way to think of model classes?
Should I avoid joining data in this way and attempting to work with nested data. Should I keep it simple and have a separate building model that I can pull buildings from when I pass in an organisation id?
Am i over thinking this? Smile

Thanks in advance codeigniter community - and go easy on me, im a n00b!


Messages In This Thread
Models, foreign keys and table associations - whats best practise in codeigniter? - by El Forum - 12-23-2009, 11:38 AM



Theme © iAndrew 2016 - Forum software by © MyBB