From e20c213ae2b1113a00766c37f96dd60a42ee82b5 Mon Sep 17 00:00:00 2001 From: mahono Date: Thu, 6 Sep 2007 14:12:40 +0000 Subject: [PATCH] refactored Table->find() --- lib/Doctrine/Table.php | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index 94db0cd85..9025a84f8 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -827,7 +827,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable */ public function hasPrimaryKey($key) { - return in_array($key,$this->primaryKeys); + return in_array($key, $this->primaryKeys); } /** * @return Doctrine_Connection @@ -853,38 +853,20 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable * finds a record by its identifier * * @param $id database row id - * @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD - * @return Doctrine_Record|false a record for given database identifier + * @param int $hydrationMode Doctrine::HYDRATE_ARRAY or Doctrine::HYDRATE_RECORD + * @return mixed Array or Doctrine_Record or false if no result */ public function find($id, $hydrationMode = null) { - if ($hydrationMode === null) { - $hydrationMode = Doctrine::FETCH_RECORD; + if (is_null($id)) { + return false; } - if ($id !== null) { - if ( ! is_array($id)) { - $id = array($id); - } else { - $id = array_values($id); - } - $records = Doctrine_Query::create() - ->from($this->getComponentName()) - ->where(implode(' = ? AND ', $this->primaryKeys) . ' = ?') - ->execute($id, $hydrationMode); + $id = is_array($id) ? array_values($id) : array($id); - if (count($records) === 0) { - return false; - } - - switch ($hydrationMode) { - case Doctrine::FETCH_RECORD: - return $records->getFirst(); - case Doctrine::FETCH_ARRAY: - return array_shift($records); - } - } - return false; + return $this->createQuery() + ->where(implode(' = ? AND ', $this->primaryKeys) . ' = ?') + ->fetchOne($id, $hydrationMode); } /** * applyInheritance