php - Data Mapper Pattern: Complexe query from Service Layer -
php - Data Mapper Pattern: Complexe query from Service Layer -
i'm sing info mapper pattern in zend framework. works far, got point need help/opinion. let's start code:
we got table several persons:
create table `persons` ( `id` int(11) not null auto_increment, `name` varchar(50) not null, `age` int(3) not null, `haircolor` varchar(20) default null, primary key (`id``), );
now seek select people, have brownish hair. utilize next method in servicelayer
public function getpeoplebyhaircolor($hair) { homecoming $this->getmapper()->fetch('haircolor = ?', $hair); }
the method in mapper looks this:
public function fetch($condition, $value) { $resultset = $this->gettable()->fetchall($this->gettable()->select()->where($cond, $value)); $entries = array(); foreach($resultset $row) { $entry = new default_model_person(); $entry->id = $row->id; $entry->name = $row->name; [...] } homecoming $entries; }
i think follow info mapper pattern methods...
now problem:
i want select persons, have brownish hair , younger 20 yrs. how can that? try:
public function getteens($hair) { $rows = $this->getmapper()->fetch('haircolor = ?', $hair); $return = array(); foreach($rows $row) { if((int)$row->age < 20) $return[] = $row; } homecoming $return; }
but if more variables, "people brownish hair, younger 20yrs , name 'foo bar'", need more , more methods and/or foreach loops.
my question:
how in info mapper pattern? if native sql query $servicelayer->mapper->table->qry('select ...'), breaking info mapper pattern? don't additional foreach loops , feels i'm doing wrong, wrote question.
indeed, 2 conditions, inadvisable query on 1 status , filter on other while iterate. leaves no clear approach more 2 conditions. , implementing pagination adapters becomes pretty messy.
seems me issue mapper fetch()
method supports permits single condition. so:
modify signature back upwards array of conditions.
alternatively, create separate mapper method each enhanced fetch method, ex: fetchbyhairandage($hair, $age)
, etc.
php design-patterns zend-framework datamapper
Comments
Post a Comment