From 82a893b06cfca24e37338442b6c3963f07092bd7 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 26 Oct 2015 12:05:37 +0100 Subject: [PATCH] DbObject: add loadWithAutoIncId --- library/Director/Data/Db/DbObject.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/library/Director/Data/Db/DbObject.php b/library/Director/Data/Db/DbObject.php index 93ac13c4..cce2bb47 100644 --- a/library/Director/Data/Db/DbObject.php +++ b/library/Director/Data/Db/DbObject.php @@ -734,6 +734,13 @@ abstract class DbObject protected function createWhere() { + if ($id = $this->getAutoincId()) { + return $this->db->quoteInto( + sprintf('%s = ?', $this->autoincKeyName), + $id + ); + } + $key = $this->getKeyName(); if (is_array($key) && ! empty($key)) { $where = array(); @@ -827,6 +834,18 @@ abstract class DbObject return $obj; } + public static function loadWithAutoIncId($id, DbConnection $connection) + { + // TODO: Index for prefetch? + + $class = get_called_class(); + $obj = new $class(); + $obj->setConnection($connection) + ->set($obj->autoincKeyName, $id) + ->loadFromDb(); + return $obj; + } + public static function load($id, DbConnection $connection) { $class = get_called_class();