php - What is the best way to solve this problem in a model? -
i come across lot, , have been curious if there better way.
consider (example only, assume oo query builder)
class dogs extends pets { public function getall() { return $this->parserows($this->db->get('dogs')); } public function getbig() { return $this->parserows($this->db->get('dogs')->where('size', '>', 10)); } public function getsmelly() { return $this->parserows($this->db->get('dogs')->where('smell', '=', 'bad')); } private function parserows($rows) { foreach($rows &$row) { $row['id'] = (int) $row['id']; $row['categoryname'] = categories::getbyid($row['categoryid']); } return $rows; } }
basically, need use lot of database queries, need go through post-processing assign things them. have been using pattern above.
is best practice way this?
to me public interface seems decent. regarding implementation, there lot said, object-relational mappers 1 of cornerstones of modern programming imho.
if learn more alternate ways of implementing orm, i'd suggest looking @ open-source frameworks such doctrine or cakephp's orm.
one note i'd add regarding private implementation: if categories::getbyid roundtrip call database, inefficient, table many dogs lead (at least) many db calls, suboptimal. why orms ones mentioned above allow developer specify associations (eg. dog has category) , automatically generate "left join" statements.
Comments
Post a Comment