public function childIds()
{
$owner = $this->getOwner();
$db = $owner->getDbConnection();
$limits = $this->treeLimits($owner);
// This was pretty much culled from the nested sets guide at:
// http://mirror.neu.edu.cn/mysql/tech-resources/articles/hierarchical-data.html
$query = 'SELECT leaf.' . $this->idAttribute . ', (COUNT(parent.' . $this->idAttribute . ') - (sub_tree.depth+1)) as depth ' . 'FROM ' . $owner->treeTable() . ' AS leaf, ' . $owner->treeTable() . ' AS parent, ' . $owner->treeTable() . ' AS sub_parent, ' . '( SELECT leaf.' . $this->idAttribute . ', (COUNT(parent.' . $this->idAttribute . ') - 1) AS depth ' . 'FROM ' . $owner->treeTable() . ' AS leaf, ' . $owner->treeTable() . ' AS parent ' . 'WHERE leaf.' . $this->leftAttribute . ' BETWEEN parent.' . $this->leftAttribute . ' AND parent.' . $this->rightAttribute . ' AND leaf.' . $this->idAttribute . ' = ' . $db->quoteValue($owner->id) . ' GROUP BY leaf.' . $this->idAttribute . ' ORDER BY leaf.' . $this->leftAttribute . ') AS sub_tree ' . 'WHERE leaf.' . $this->leftAttribute . ' BETWEEN parent.' . $this->leftAttribute . ' AND parent.' . $this->rightAttribute . ' AND leaf.' . $this->leftAttribute . ' BETWEEN sub_parent.' . $this->leftAttribute . ' AND sub_parent.' . $this->rightAttribute . ' AND sub_parent.' . $this->idAttribute . ' = sub_tree.' . $this->idAttribute . ' ' . 'GROUP BY leaf.' . $this->idAttribute . ' HAVING depth = ' . '(SELECT count(*) FROM ' . $owner->treeTable() . ' AS tree WHERE tree.' . $this->idAttribute . ' = ' . $db->quoteValue($owner->id) . ') ' . 'ORDER BY leaf.' . $this->leftAttribute;
$res = $db->createCommand($query)->query();
$result = array();
foreach ($res as $r) {
$result[] = $r['id'];
}
return $result;
}