示例#1
0
 /**
  * Fetchs one entity by Query object
  * @return Entity|FALSE
  */
 public function findOne($field, IQuery $query)
 {
     $query->limit(1);
     $entities = $this->queryProperty($field, $query);
     if ($entities) {
         return $entities[0];
     }
     return FALSE;
 }
示例#2
0
 protected function queryProperty($field, IQuery $query)
 {
     if ($this->isDetached()) {
         throw new InvalidStateException('Cannot query detached entity.');
     }
     $property = $this->getCurrentReflection()->getEntityProperty($field);
     if ($property === NULL) {
         throw new MemberAccessException("Cannot access undefined property '{$field}' in entity " . get_called_class() . '.');
     }
     if (!$property->hasRelationship()) {
         throw new InvalidArgumentException("Property '{$property->getName()}' in entity " . get_called_class() . " has no relationship.");
     }
     $class = $property->getType();
     $filters = $this->createImplicitFilters($class, new Caller($this, $property))->getFilters();
     $mapper = $this->mapper;
     $filters[] = function (Fluent $fluent) use($mapper, $query) {
         $query->applyQuery($fluent, $mapper);
     };
     $relationship = $property->getRelationship();
     if ($relationship instanceof Relationship\BelongsToMany) {
         $targetTable = $relationship->getTargetTable();
         $referencingColumn = $relationship->getColumnReferencingSourceTable();
         $rows = $this->row->referencing($targetTable, $referencingColumn, new Filtering($filters));
     } elseif ($relationship instanceof Relationship\HasMany) {
         $relationshipTable = $relationship->getRelationshipTable();
         $sourceReferencingColumn = $relationship->getColumnReferencingSourceTable();
         $targetReferencingColumn = $relationship->getColumnReferencingTargetTable();
         $targetTable = $relationship->getTargetTable();
         $rows = array();
         foreach ($this->row->referencing($relationshipTable, $sourceReferencingColumn) as $relationship) {
             $row = $relationship->referenced($targetTable, $targetReferencingColumn, new Filtering($filters));
             $row !== NULL && ($rows[] = $row);
         }
     } else {
         throw new InvalidRelationshipException('Only BelongsToMany and HasMany relationships are supported when querying entity property. ' . get_class($relationship) . ' given.');
     }
     $entities = array();
     foreach ($rows as $row) {
         $entity = $this->entityFactory->createEntity($class, $row);
         $entity->makeAlive($this->entityFactory);
         $entities[] = $entity;
     }
     return $entities;
 }
示例#3
0
 /**
  * @return Fluent
  */
 protected function applyQuery(IQuery $query)
 {
     $fluent = $this->createFluent();
     $query->applyQuery($fluent, $this->mapper);
     return $fluent;
 }