protected function &_queryExport($query)
{
$data = $query->export($this);
if (!$query->limit() || !($model = $query->model())) {
return $data;
}
foreach ($query->relationships() as $relation) {
if ($relation['type'] !== 'hasMany') {
continue;
}
$pk = $this->name($model::meta('name') . '.' . $model::key());
$result = $this->_execute($this->renderCommand('read', array('fields' => "DISTINCT({$pk}) AS _ID_") + $data));
$ids = array();
foreach ($result as $row) {
$ids[] = $row[0];
}
if (!$ids) {
$data = null;
break;
}
$conditions = array();
$relations = array_keys($query->relationships());
$pattern = '/^(' . implode('|', $relations) . ')\\./';
foreach ($query->conditions() as $key => $value) {
if (preg_match($pattern, $key)) {
$conditions[$key] = $value;
}
}
$data['conditions'] = $this->conditions(array($pk => $ids) + $conditions, $query);
$data['limit'] = '';
break;
}
return $data;
}