Esempio n. 1
0
File: Model.php Progetto: ssrsfs/blg
 /**
  * Fetch a single record by its primary key.
  * @param scalar|array $key The primary key value. If the primary
  * key contains more than one column, use an associative array.
  * @return Dbi_Record
  */
 public static function Get($key)
 {
     $cls = get_called_class();
     $model = new $cls();
     $primary = $model->index('primary');
     if (is_null($primary)) {
         throw new Exception("The schema for {$cls} does not identify a primary key");
     }
     if (!is_array($key)) {
         if (count($primary['fields']) > 1) {
             throw new Exception("The schema for {$cls} has more than one field in its primary key");
         }
         $key = array($primary['fields'][0] => $key);
     }
     foreach ($primary['fields'] as $field) {
         if (!isset($key[$field])) {
             throw new Exception("No value provided for the {$field} field in the primary key for " . get_called_class());
         }
         $model->where("{$field} = ?", $key[$field]);
     }
     //$src = Dbi_Source::GetModelSource($model);
     $result = $model->select();
     if ($result->count()) {
         if ($result->count() > 1) {
             throw new Exception("{$cls} returned multiple records for primary key {$id}");
         }
         $record = $result[0];
     } else {
         $record = new Dbi_Record($model, null);
         $record->setArray($key, false);
     }
     return $record;
 }