Preparing newest for more functionality

This commit is contained in:
Dennis Eichhorn 2016-01-17 20:36:21 +01:00
parent d4321df72f
commit 4c9fca4f62

View File

@ -512,22 +512,25 @@ abstract class DataMapperAbstract implements DataMapperInterface
* *
* This will fall back to the insert id if no datetime column is present. * This will fall back to the insert id if no datetime column is present.
* *
* @param \int $limit Newest limit
* @param Builder $query Pre-defined query
*
* @return mixed * @return mixed
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function getNewest() public function getNewest(\int $limit = 1, Builder $query = null)
{ {
if (!isset(static::$createdAt) || !isset(static::$columns[static::$createdAt])) { if (!isset(static::$createdAt) || !isset(static::$columns[static::$createdAt])) {
throw new \BadMethodCallException('Method "' . __METHOD__ . '" is not supported.'); throw new \BadMethodCallException('Method "' . __METHOD__ . '" is not supported.');
} }
$query = new Builder($this->db); $query = $query ?? new Builder($this->db);
$query->prefix($this->db->getPrefix()) $query->prefix($this->db->getPrefix())
->select('*') ->select('*')
->from(static::$table) ->from(static::$table)
->limit(1); ->limit($limit); /* todo: limit is not working, setting this to 2 doesn't have any effect!!! */
if (isset(static::$createdAt)) { if (isset(static::$createdAt)) {
$query->orderBy(static::$table . '.' . static::$columns[static::$createdAt]['name'], 'DESC'); $query->orderBy(static::$table . '.' . static::$columns[static::$createdAt]['name'], 'DESC');
@ -540,6 +543,7 @@ abstract class DataMapperAbstract implements DataMapperInterface
$results = $sth->fetch(\PDO::FETCH_ASSOC); $results = $sth->fetch(\PDO::FETCH_ASSOC);
/* todo: if limit get's used this has to call populateIterable */
return $this->populate(is_bool($results) ? [] : $results); return $this->populate(is_bool($results) ? [] : $results);
} }