/**
* Returns referenced row.
* @param ActiveRow
* @param string
* @param string|NULL
* @return ActiveRow|NULL|FALSE NULL if the row does not exist, FALSE if the relationship does not exist
*/
public function getReferencedTable(ActiveRow $row, $table, $column = NULL)
{
if (!$column) {
$belongsTo = $this->conventions->getBelongsToReference($this->name, $table);
if (!$belongsTo) {
return FALSE;
}
list($table, $column) = $belongsTo;
}
if (!$row->accessColumn($column)) {
return FALSE;
}
$checkPrimaryKey = $row[$column];
$referenced =& $this->refCache['referenced'][$this->getSpecificCacheKey()]["{$table}.{$column}"];
$selection =& $referenced['selection'];
$cacheKeys =& $referenced['cacheKeys'];
if ($selection === NULL || $checkPrimaryKey !== NULL && !isset($cacheKeys[$checkPrimaryKey])) {
$this->execute();
$cacheKeys = array();
foreach ($this->rows as $row) {
if ($row[$column] === NULL) {
continue;
}
$key = $row[$column];
$cacheKeys[$key] = TRUE;
}
if ($cacheKeys) {
$selection = $this->createSelectionInstance($table);
$selection->where($selection->getPrimary(), array_keys($cacheKeys));
} else {
$selection = array();
}
}
return isset($selection[$checkPrimaryKey]) ? $selection[$checkPrimaryKey] : NULL;
}