I am using CI, however this question applies to models and db persistence in general. I find myself creating methods in models like:-
private function create_joins() {
# Add some joins to the global db object using the active record
}
I do this so I can then perform common joins for a particular model without duplicating the code for creating the joins.
So a select method might be:-
public function get_by_id($id) {
$this->db->select('some_cols');
$this->db->from('some_table');
$this->create_joins();
$this->db->where(array('id' => $id));
etc...
}
Which is fine, but I am wondering if this is the sort of thing that an ORM like datamapper can abstract away?
You should try Doctrine, which is one of the most advanced ORM in PHP:
Using Doctrine, you won’t even have to write methods such as
get_by_id($id)in your model : they are handled by Doctrine itself.So you would be able to write: