Esempio n. 1
0
 /**
  * lookup
  *
  * Retrieve a record by its primary key. This method may be short
  * circuited by model caching if the record has already been loaded by
  * the database. In such a case, the database will not be consulted for
  * the model's data.
  *
  * This method can be called with an array of keyword arguments matching
  * the PK of the object or the values of the primary key. Both of these
  * usages are correct:
  *
  * >>> User::lookup(1)
  * >>> User::lookup(array('id'=>1))
  *
  * For composite primary keys and the first usage, pass the values in
  * the order they are given in the Model's 'pk' declaration in its meta
  * data. For example:
  *
  * >>> UserPrivilege::lookup(1, 2)
  *
  * Parameters:
  * $criteria - (mixed) primary key for the sought model either as
  *      arguments or key/value array as the function's first argument
  *
  * Returns:
  * (Object<ModelBase>|null) a single instance of the sought model or
  * null if no such instance exists.
  *
  * Throws:
  * Db\Exception\NotUnique if the criteria does not hit a single object
  */
 static function lookup($criteria)
 {
     // Model::lookup(1), where >1< is the pk value
     if (!is_array($criteria)) {
         $criteria = array();
         $pk = static::getMeta('pk');
         foreach (func_get_args() as $i => $f) {
             $criteria[$pk[$i]] = $f;
         }
         // Only consult cache for PK lookup, which is assumed if the
         // values are passed as args rather than an array
         if ($cached = ModelInstanceManager::checkCache(get_called_class(), $criteria)) {
             return $cached;
         }
     }
     return static::objects()->filter($criteria)->one();
 }