public function mapFields($object, $fields = array())
{
$relationships = array();
foreach ($fields as $fieldName => &$fieldValue) {
if (strpos($fieldName, '@') !== false) {
list($rel, $field) = explode('@', $fieldName, 2);
$relationships[$rel][$field] = $fieldValue;
unset($fields[$fieldName]);
}
if (isset($this->fields[$fieldName])) {
$fieldName = $this->fields[$fieldName];
}
$column = $this->tableDefinition->getColumn($fieldName);
if ($column) {
$fieldValue = $column->typeCast($fieldValue);
}
}
$object->setFields($fields);
if (count($relationships)) {
foreach ($this->relationships as $relationship => $rel) {
if (isset($rel['mapper'])) {
if ($this->_factory) {
$m = $this->_factory->create($rel['mapper']);
} else {
$m = new $rel['mapper']($this->adapter);
}
} else {
$m = $this->tableToMapper($relationship);
if (is_null($m)) {
// @TODO Throw an exception?
continue;
}
}
// Don't check the table only. If there was a match for LEFT
// JOINs, there may be a "empty" result
if (isset($relationships[$m->table], $relationships[$m->table][$m->primaryKey])) {
$object->{$relationship} = $m->map($relationships[$m->table]);
}
}
}
}