Beispiel #1
0
 /**
  * Creates an array criteria for a key index
  * @param mixed $meta Meta or class name
  * @return array Criteria
  */
 public function createKeyCriteria($meta, $id, $indexId = null)
 {
     if ($indexId == null) {
         $indexId = 'primary';
     }
     $meta = !$meta instanceof Meta ? $this->mapper->getMeta($meta) : $meta;
     if (!isset($meta->indexes[$indexId])) {
         throw new Exception("Index {$indexId} does not exist on meta {$meta->id}");
     }
     $index = $meta->indexes[$indexId];
     if (!$index['key']) {
         throw new Exception("Index {$indexId} is not a key index for meta {$meta->id}");
     }
     if (!is_array($id)) {
         $id = array($id);
     }
     $where = array();
     foreach ($index['fields'] as $idx => $p) {
         $idVal = isset($id[$p]) ? $id[$p] : (isset($id[$idx]) ? $id[$idx] : null);
         if (!$idVal) {
             throw new \InvalidArgumentException("Couldn't get ID value when getting {$meta->id} by index '{$indexId}'");
         }
         $where[$p] = $idVal;
     }
     return array('where' => $where);
 }
Beispiel #2
0
 public function __construct(Mapper $mapper, $class)
 {
     if (!$this->engine) {
         throw new \UnexpectedValueException();
     }
     $this->mapper = $mapper;
     if ($class instanceof Meta) {
         $this->meta = $class;
     } else {
         $this->meta = $mapper->getMeta($class);
     }
 }