/** * Select a single instance. * * @param mixed $scheme Can be the name of a One_Scheme or the One_Scheme object itself * @param mixed $identityValue * @return One_Model */ public function selectOne(One_Scheme_Interface $scheme, $identityValue) { $cached = One_Model_IdentityMap::find($scheme->getName(), $identityValue); if ($cached) { return $cached; } $db = $this->db($scheme); $renderer = $this->getRenderer(); $query = One_Repository::selectQuery($scheme->getName()); $idAttr = $scheme->getIdentityAttribute(); $column = $idAttr->getName(); $value = $idAttr->toString($identityValue); $query->where($column, 'eq', $value); $result = $this->executeSchemeQuery($query); return count($result) > 0 ? $result[0] : NULL; }
/** * Select a single instance. * * @param mixed $scheme Can be the name of a One_Scheme or the One_Scheme object itself * @param mixed $identityValue * @return One_Model */ public function selectOne(One_Scheme $scheme, $identityValue) { $cached = One_Model_IdentityMap::find($scheme->getName(), $identityValue); if ($cached) { return $cached; } $db = $this->db($scheme); $connectionMeta = $scheme->getResources(); $collection = $db->selectCollection($connectionMeta['collection']); try { $result = $collection->findOne(array('_id' => new MongoId($identityValue))); } catch (Exception $e) { throw new One_Exception('Query failed: ' . $e->getMessage()); } if (null !== $result) { $result = $this->arrayToInstance($scheme, $result); } return $result; }