Ejemplo n.º 1
0
 /**
  * @param TokenSequencerInterface $query
  * @param $condition
  * @param bool|false $prefixFieldWithCollection
  * @param bool|true $addLogicOperator
  * @throws RepositoryException
  */
 protected function addConditionToQuery(TokenSequencerInterface $query, $condition, $prefixFieldWithCollection = false, $addLogicOperator = true)
 {
     // normalise $condition
     $field = $condition["field"];
     $op = strtolower($condition["op"]);
     $value = $condition["value"];
     $inverted = !empty($condition["inverted"]);
     $related = $field;
     if ($query->getEntityMetadata()->hasRelationship($field)) {
         $relationship = $query->getEntityMetadata()->getRelationship($field);
         if (empty($relationship) || $relationship[EntityMetadata::METADATA_RELATIONSHIP_TYPE] == EntityMetadata::RELATIONSHIP_TYPE_ONE_TO_MANY) {
             $ourField = null;
         } else {
             $ourField = empty($relationship[EntityMetadata::METADATA_RELATIONSHIP_OUR_FIELD]) ? null : $relationship[EntityMetadata::METADATA_RELATIONSHIP_OUR_FIELD];
             if (empty($ourField) && $relationship[EntityMetadata::METADATA_RELATIONSHIP_TYPE] == EntityMetadata::RELATIONSHIP_TYPE_MANY_TO_MANY) {
                 // our field is the join collection field which references the foreign collection
                 $joinCollection = $relationship[EntityMetadata::METADATA_RELATIONSHIP_JOIN_TABLE];
                 $childMetadata = $this->metadataProvider->getEntityMetadata($relationship[EntityMetadata::METADATA_ENTITY]);
                 $ourField = $joinCollection . "." . $childMetadata->getCollection() . "_" . $childMetadata->getPrimaryKey();
             }
         }
         if (empty($ourField)) {
             throw new RepositoryException("No field could be found for the relationship '{$field}'");
         }
         $field = $ourField;
         $related = $relationship;
     }
     // if any value is an entity, get the value from the entity for the field we're looking for
     if (is_array($value)) {
         foreach ($value as $i => $entity) {
             $value[$i] = $this->getRelatedValueFromEntity($entity, $related);
         }
     } else {
         $value = $this->getRelatedValueFromEntity($value, $related);
     }
     if ($prefixFieldWithCollection && strpos($field, ".") === false) {
         $field = $this->collectionName . "." . $field;
     }
     // sprinkle with logic operators to taste
     if ($addLogicOperator) {
         // We do not need to allow for the OR operator. Complex queries that require OR are best written in custom methods
         $query->andL();
     }
     if ($inverted) {
         $query->notL();
     }
     // add condition to the query
     $query->ref($field)->op($op);
     switch ($op) {
         case "in":
             $query->closure($value);
             break;
         case "between":
             $query->val($value[0])->andL()->val($value[1]);
             break;
         default:
             $query->val($value);
             break;
     }
 }