Commit e20c213a authored by mahono's avatar mahono

refactored Table->find()

parent 814223bd
...@@ -827,7 +827,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -827,7 +827,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
*/ */
public function hasPrimaryKey($key) public function hasPrimaryKey($key)
{ {
return in_array($key,$this->primaryKeys); return in_array($key, $this->primaryKeys);
} }
/** /**
* @return Doctrine_Connection * @return Doctrine_Connection
...@@ -853,38 +853,20 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -853,38 +853,20 @@ 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 * @param int $hydrationMode Doctrine::HYDRATE_ARRAY or Doctrine::HYDRATE_RECORD
* @return Doctrine_Record|false a record for given database identifier * @return mixed Array or Doctrine_Record or false if no result
*/ */
public function find($id, $hydrationMode = null) public function find($id, $hydrationMode = null)
{ {
if ($hydrationMode === null) { if (is_null($id)) {
$hydrationMode = Doctrine::FETCH_RECORD; return false;
} }
if ($id !== null) {
if ( ! is_array($id)) {
$id = array($id);
} else {
$id = array_values($id);
}
$records = Doctrine_Query::create() $id = is_array($id) ? array_values($id) : array($id);
->from($this->getComponentName())
->where(implode(' = ? AND ', $this->primaryKeys) . ' = ?')
->execute($id, $hydrationMode);
if (count($records) === 0) { return $this->createQuery()
return false; ->where(implode(' = ? AND ', $this->primaryKeys) . ' = ?')
} ->fetchOne($id, $hydrationMode);
switch ($hydrationMode) {
case Doctrine::FETCH_RECORD:
return $records->getFirst();
case Doctrine::FETCH_ARRAY:
return array_shift($records);
}
}
return false;
} }
/** /**
* applyInheritance * applyInheritance
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment