Skip to content

Instantly share code, notes, and snippets.

@jamierumbelow
Created October 1, 2011 09:55
Show Gist options
  • Save jamierumbelow/1255824 to your computer and use it in GitHub Desktop.
Save jamierumbelow/1255824 to your computer and use it in GitHub Desktop.
A simple concept for basic JOIN/relationship support in MY_Model
<?php
class Customer extends MY_Model {
protected $relations = array(
'addresses' => array(
'join_on' => 'addressable_id',
'where' => array('addresses.addressable_type' => 'customers')
)
);
}
// This...
$this->db->select('customers.*, addresses.*')->join('addresses', 'addresses.addressable_id = customers.id')->where('addresses.addressable_type', 'customers')->get();
// ...becomes this.
$this->customer->with_relation('addresses')->get_all();
<?php
class User extends MY_Model {
protected $relations = array(
'books' => 'id'
);
}
// This...
$this->db->select('users.*, books.*')->join('books', 'books.id = users.id')->get();
// ...becomes this.
$this->user->with_relation('books')->get_all();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment