public function load($args, array $options = null)
{
if (!$this->currentUserCan($this->getCurrentUser(), 'load', $args)) {
return $this->reportError('Permission denied. Can not load record.', array('args' => $args));
}
$dsId = $this->readSourceId;
$pk = static::PRIMARY_KEY;
$query = new SelectQuery();
$query->from($this->table, $this->alias);
$conn = $this->getReadConnection();
$driver = $this->getReadQueryDriver();
$kVal = null;
// build query from array.
if (is_array($args)) {
$query->select($this->selected ?: '*')->where($args);
} else {
$kVal = $args;
$column = $this->getSchema()->getColumn($pk);
if (!$column) {
// This should not happend, every schema should have it's own primary key
// TODO: Create new exception class for this.
throw new MissingPrimaryKeyException($this->getSchema(), "Primary key {$pk} is not defined");
}
$kVal = $column->deflate($kVal);
$args = array($pk => $kVal);
$query->select($this->selected ?: '*')->where($args);
}
// generate select * ... for update syntax for MySQL driver
if (isset($options['for_update']) && $driver instanceof PDOMySQLDriver) {
$query->forUpdate();
}
$arguments = new ArgumentArray();
$sql = $query->toSql($driver, $arguments);
// mixed PDOStatement::fetch ([ int $fetch_style [, int $cursor_orientation = PDO::FETCH_ORI_NEXT [, int $cursor_offset = 0 ]]] )
$stm = $conn->prepare($sql);
$stm->execute($arguments->toArray());
if (false === ($this->_data = $stm->fetch(PDO::FETCH_ASSOC))) {
// Record not found is not an exception
return $this->reportError('Record not found', ['sql' => $sql]);
}
return $this->reportSuccess('Data loaded', array('id' => isset($this->_data[$pk]) ? $this->_data[$pk] : null, 'sql' => $sql, 'type' => Result::TYPE_LOAD));
}