public function find($keys = null)
{
// Execute the onBeforeLoad event
$this->triggerEvent('onBeforeLoad', array(&$keys));
// If we are not given any keys, try to get the ID from the state or the table data
if (empty($keys)) {
$id = $this->getState('id', 0);
if (empty($id)) {
$id = $this->getId();
}
if (empty($id)) {
$this->triggerEvent('onAfterLoad', array(false, &$keys));
$this->reset();
return $this;
}
$keys = array($this->idFieldName => $id);
} elseif (!is_array($keys)) {
if (empty($keys)) {
$this->triggerEvent('onAfterLoad', array(false, &$keys));
$this->reset();
return $this;
}
$keys = array($this->idFieldName => $keys);
}
// Reset the table
$this->reset();
// Get the query
$db = $this->getDbo();
$query = $db->getQuery(true)->select('*')->from($db->qn($this->tableName));
// Apply key filters
foreach ($keys as $filterKey => $filterValue) {
if ($filterKey == 'id') {
$filterKey = $this->getIdFieldName();
}
if (array_key_exists($filterKey, $this->recordData)) {
$query->where($db->qn($filterKey) . ' = ' . $db->q($filterValue));
}
}
// Get the row
$db->setQuery($query);
try {
$row = $db->loadAssoc();
} catch (\Exception $e) {
$row = null;
}
if (empty($row)) {
$this->triggerEvent('onAfterLoad', array(false, &$keys));
return $this;
}
// Bind the data
$this->bind($row);
$this->relationManager->rebase($this);
// Execute the onAfterLoad event
$this->triggerEvent('onAfterLoad', array(true, &$keys));
return $this;
}