1
0
mirror of synced 2024-12-14 15:16:04 +03:00

Added FETCH_ARRAY support for table finder methods, fixes bug #397

This commit is contained in:
jackbravo 2007-09-01 19:44:38 +00:00
parent 202d66860b
commit 9b5246368e
3 changed files with 32 additions and 15 deletions

View File

@ -677,14 +677,15 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
* *
* @param string $query DQL query * @param string $query DQL query
* @param array $params query parameters * @param array $params query parameters
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
* @see Doctrine_Query * @see Doctrine_Query
* @return Doctrine_Collection Collection of Doctrine_Record objects * @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); $parser = new Doctrine_Query($this);
return $parser->query($query, $params); return $parser->query($query, $params, $hydrationMode);
} }
/** /**
* prepare * prepare

View File

@ -1520,16 +1520,17 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
* query * query
* query the database with DQL (Doctrine Query Language) * query the database with DQL (Doctrine Query Language)
* *
* @param string $query DQL query * @param string $query DQL query
* @param array $params prepared statement parameters * @param array $params prepared statement parameters
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
* @see Doctrine::FETCH_* constants * @see Doctrine::FETCH_* constants
* @return mixed * @return mixed
*/ */
public function query($query, $params = array()) public function query($query, $params = array(), $hydrationMode = null)
{ {
$this->parseQuery($query); $this->parseQuery($query);
return $this->execute($params); return $this->execute($params, $hydrationMode);
} }
public function copy(Doctrine_Query $query = null) public function copy(Doctrine_Query $query = null)

View File

@ -853,10 +853,14 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
* finds a record by its identifier * finds a record by its identifier
* *
* @param $id database row id * @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 * @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 ($id !== null) {
if ( ! is_array($id)) { if ( ! is_array($id)) {
$id = array($id); $id = array($id);
@ -867,13 +871,22 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
$records = Doctrine_Query::create() $records = Doctrine_Query::create()
->from($this->getComponentName()) ->from($this->getComponentName())
->where(implode(' = ? AND ', $this->primaryKeys) . ' = ?') ->where(implode(' = ? AND ', $this->primaryKeys) . ' = ?')
->execute($id); ->execute($id, $hydrationMode);
if (count($records) === 0) { if (count($records) === 0) {
return false; 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; return false;
} }
@ -898,12 +911,13 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
* findAll * findAll
* returns a collection of records * returns a collection of records
* *
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
* @return Doctrine_Collection * @return Doctrine_Collection
*/ */
public function findAll() public function findAll($hydrationMode = null)
{ {
$graph = new Doctrine_Query($this->conn); $graph = new Doctrine_Query($this->conn);
$users = $graph->query('FROM ' . $this->options['name']); $users = $graph->query('FROM ' . $this->options['name'], array(), $hydrationMode);
return $users; return $users;
} }
/** /**
@ -913,16 +927,17 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
* *
* @param string $dql DQL after WHERE clause * @param string $dql DQL after WHERE clause
* @param array $params query parameters * @param array $params query parameters
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
* @return Doctrine_Collection * @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); $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; return $users;
} }
public function findByDql($dql, array $params = array()) { public function findByDql($dql, array $params = array(), $hydrationMode = null) {
return $this->findBySql($dql, $params); return $this->findBySql($dql, $params, $hydrationMode);
} }
/** /**
* clear * clear