From 9b5246368e91db44d5cabd3baf6ff017abc0039b Mon Sep 17 00:00:00 2001 From: jackbravo Date: Sat, 1 Sep 2007 19:44:38 +0000 Subject: [PATCH] Added FETCH_ARRAY support for table finder methods, fixes bug #397 --- lib/Doctrine/Connection.php | 5 +++-- lib/Doctrine/Query.php | 9 +++++---- lib/Doctrine/Table.php | 33 ++++++++++++++++++++++++--------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/Doctrine/Connection.php b/lib/Doctrine/Connection.php index f7b4ee16c..de4c47e5a 100644 --- a/lib/Doctrine/Connection.php +++ b/lib/Doctrine/Connection.php @@ -677,14 +677,15 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * * @param string $query DQL query * @param array $params query parameters + * @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD * @see Doctrine_Query * @return Doctrine_Collection Collection of Doctrine_Record objects */ - public function query($query, array $params = array()) + public function query($query, array $params = array(), $hydrationMode = null) { $parser = new Doctrine_Query($this); - return $parser->query($query, $params); + return $parser->query($query, $params, $hydrationMode); } /** * prepare diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index f084af5cb..10d575635 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -1520,16 +1520,17 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable * query * query the database with DQL (Doctrine Query Language) * - * @param string $query DQL query - * @param array $params prepared statement parameters + * @param string $query DQL query + * @param array $params prepared statement parameters + * @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD * @see Doctrine::FETCH_* constants * @return mixed */ - public function query($query, $params = array()) + public function query($query, $params = array(), $hydrationMode = null) { $this->parseQuery($query); - return $this->execute($params); + return $this->execute($params, $hydrationMode); } public function copy(Doctrine_Query $query = null) diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index 2d5cf0326..03039c1ab 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -853,10 +853,14 @@ 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 */ - public function find($id) + public function find($id, $hydrationMode = null) { + if ($hydrationMode === null) { + $hydrationMode = Doctrine::FETCH_RECORD; + } if ($id !== null) { if ( ! is_array($id)) { $id = array($id); @@ -867,13 +871,22 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable $records = Doctrine_Query::create() ->from($this->getComponentName()) ->where(implode(' = ? AND ', $this->primaryKeys) . ' = ?') - ->execute($id); + ->execute($id, $hydrationMode); if (count($records) === 0) { return false; } - return $records->getFirst(); + switch ($hydrationMode) { + case Doctrine::FETCH_RECORD: + if (count($records) > 0) { + return $records->getFirst(); + } + case Doctrine::FETCH_ARRAY: + if (!empty($records[0])) { + return $records[0]; + } + } } return false; } @@ -898,12 +911,13 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable * findAll * returns a collection of records * + * @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD * @return Doctrine_Collection */ - public function findAll() + public function findAll($hydrationMode = null) { $graph = new Doctrine_Query($this->conn); - $users = $graph->query('FROM ' . $this->options['name']); + $users = $graph->query('FROM ' . $this->options['name'], array(), $hydrationMode); return $users; } /** @@ -913,16 +927,17 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable * * @param string $dql DQL after WHERE clause * @param array $params query parameters + * @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD * @return Doctrine_Collection */ - public function findBySql($dql, array $params = array()) { + public function findBySql($dql, array $params = array(), $hydrationMode = null) { $q = new Doctrine_Query($this->conn); - $users = $q->query('FROM ' . $this->options['name'] . ' WHERE ' . $dql, $params); + $users = $q->query('FROM ' . $this->options['name'] . ' WHERE ' . $dql, $params, $hydrationMode); return $users; } - public function findByDql($dql, array $params = array()) { - return $this->findBySql($dql, $params); + public function findByDql($dql, array $params = array(), $hydrationMode = null) { + return $this->findBySql($dql, $params, $hydrationMode); } /** * clear