1
0
mirror of synced 2025-02-11 17:59:27 +03:00

* added new fetchOne() method for convenience

* some whitespace cleanups
This commit is contained in:
mahono 2007-07-27 15:58:37 +00:00
parent 97e82445d7
commit c662f4d99f

View File

@ -230,7 +230,7 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
*/ */
public function useCache($driver = true, $timeToLive = null) public function useCache($driver = true, $timeToLive = null)
{ {
if ($driver !== null) { if ($driver !== null) {
if ($driver !== true) { if ($driver !== true) {
if ( ! ($driver instanceof Doctrine_Cache_Interface)) { if ( ! ($driver instanceof Doctrine_Cache_Interface)) {
$msg = 'First argument should be instance of Doctrine_Cache_Interface or null.'; $msg = 'First argument should be instance of Doctrine_Cache_Interface or null.';
@ -238,7 +238,7 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
throw new Doctrine_Hydrate_Exception($msg); throw new Doctrine_Hydrate_Exception($msg);
} }
} }
} }
$this->_cache = $driver; $this->_cache = $driver;
return $this->setCacheLifeSpan($timeToLive); return $this->setCacheLifeSpan($timeToLive);
@ -263,9 +263,9 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
*/ */
public function setCacheLifeSpan($timeToLive) public function setCacheLifeSpan($timeToLive)
{ {
if ($timeToLive !== null) { if ($timeToLive !== null) {
$timeToLive = (int) $timeToLive; $timeToLive = (int) $timeToLive;
} }
$this->_timeToLive = $timeToLive; $this->_timeToLive = $timeToLive;
return $this; return $this;
@ -278,11 +278,11 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
*/ */
public function getCacheDriver() public function getCacheDriver()
{ {
if ($this->_cache instanceof Doctrine_Cache_Interface) { if ($this->_cache instanceof Doctrine_Cache_Interface) {
return $this->_cache; return $this->_cache;
} else { } else {
return $this->_conn->getCacheDriver(); return $this->_conn->getCacheDriver();
} }
} }
/** /**
* Sets the fetchmode. * Sets the fetchmode.
@ -776,8 +776,8 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
*/ */
public function execute($params = array(), $hydrationMode = null) public function execute($params = array(), $hydrationMode = null)
{ {
if ($this->_cache) { if ($this->_cache) {
$cacheDriver = $this->getCacheDriver(); $cacheDriver = $this->getCacheDriver();
$dql = $this->getDql(); $dql = $this->getDql();
// calculate hash for dql query // calculate hash for dql query
@ -904,9 +904,46 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
return $str; return $str;
} }
/**
* fetchArray
* Convenience method to execute using array fetching as hydration mode.
*
* @param string $params
* @return array
*/
public function fetchArray($params = array()) { public function fetchArray($params = array()) {
return $this->execute($params, self::HYDRATE_ARRAY); return $this->execute($params, self::HYDRATE_ARRAY);
} }
/**
* fetchOne
* Convenience method to execute the query and return the first item
* of the collection.
*
* @param string $params Parameters
* @param int $hydrationMode Hydration mode
* @return mixed Array or Doctrine_Collection or false if no result.
*/
public function fetchOne($params = array(), $hydrationMode = null)
{
if (is_null($hydrationMode)) {
$hydrationMode = $this->_hydrationMode;
}
$collection = $this->execute($params, $hydrationMode);
switch ($hydrationMode) {
case self::HYDRATE_RECORD:
if (count($collection) > 0) {
return $collection->getFirst();
}
case self::HYDRATE_ARRAY:
if (!empty($collection[0])) {
return $collection[0];
}
}
return false;
}
/** /**
* parseData * parseData
* parses the data returned by statement object * parses the data returned by statement object
@ -1141,9 +1178,9 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
*/ */
public function _setLastElement(&$prev, &$coll, $index, $alias, $oneToOne) public function _setLastElement(&$prev, &$coll, $index, $alias, $oneToOne)
{ {
if ($coll === self::$_null) { if ($coll === self::$_null) {
return false; return false;
} }
if ($index !== false) { if ($index !== false) {
$prev[$alias] =& $coll[$index]; $prev[$alias] =& $coll[$index];
} else { } else {