function execute()
{
if (isset($this->rows)) {
return $this;
}
if ($this->parent_) {
// restrict to parent
$this->where[] = $this->db->is($this->key, $this->parent_->getGlobalKeys($this->parentKey));
}
$root = $this->getRoot();
$definition = $this->getDefinition();
$cached = $root->getCache($definition);
if (!$cached) {
// fetch all rows
$statement = $this->db->select($this->table, array('expr' => $this->select, 'where' => $this->where, 'orderBy' => $this->orderBy, 'limitCount' => $this->limitCount, 'limitOffset' => $this->limitOffset, 'params' => $this->whereParams));
$rows = $statement->fetchAll();
$cached = array();
// build row objects
foreach ($rows as $row) {
$row = $this->createRow($row);
$row->setClean();
$cached[] = $row;
}
$root->setCache($definition, $cached);
}
$this->globalRows = $cached;
if (!$this->parent_) {
$this->rows = $cached;
} else {
$this->rows = array();
$keys = $this->parent_->getLocalKeys($this->parentKey);
foreach ($cached as $row) {
if (in_array($row->__get($this->key), $keys)) {
$this->rows[] = $row;
}
}
}
return $this;
}