Beispiel #1
0
 /**
  * Shortcut method to register a reference from the condition to resolve sub_queries
  *
  * @param string       $attribute
  * @param string|array $target_s
  * @param array|null   $on
  * @return $this
  */
 public function reference($attribute, $target_s, $on = null)
 {
     if ($this->_collection_name) {
         \Rocketr\Schema\Reference::add($this->_collection_name, [$attribute => $target_s], $on);
     } else {
         //TODO: Exeption ?
     }
     return $this;
 }
Beispiel #2
0
 /**
  * Transform the value in a MongoDBRef if needed
  * @param $attr
  * @param $value
  * @return mixed
  */
 protected function _val($attr, $value)
 {
     $reference = \Rocketr\Schema\Reference::get($this->get_collection_name(), $attr);
     $collection = \Rocketr\Schema\HasCollection::get($this->get_collection_name(), $attr);
     if ($reference xor $collection) {
         $ref_attr = \Rocketr\Schema\Map::on($this->get_collection_name(), $attr);
         $_id = is_array($value) && isset($value[$ref_attr]) || is_object($value) && $value->{$ref_attr} ? is_array($value) ? $value[$ref_attr] : $value->{$ref_attr} : $value;
         $target = $reference ? $reference : $collection;
         $_id = $ref_attr == '_id' && \MongoId::isValid($_id) ? new \MongoId($_id) : $_id;
         return \MongoDBRef::create($target, $_id);
     } else {
         return $value;
     }
 }
Beispiel #3
0
 /**
  * @param $which
  * @param $collection
  * @param $attr
  * @param $target
  */
 private function _push_to_map($which, $collection, $attr, $target)
 {
     if ($which == 'reference') {
         $ref_attr = isset($on[$collection][$attr]) ? $on[$collection][$attr] : null;
         \Rocketr\Schema\Reference::add($collection, [$attr => $target], $ref_attr);
     } else {
         if ($which == 'has_collection') {
             $ref_attr = isset($on[$collection][$attr]) ? $on[$collection][$attr] : null;
             \Rocketr\Schema\HasCollection::add($collection, [$attr => $target], $ref_attr);
         }
     }
 }
Beispiel #4
0
 /**
  * Use Reference defined in Schema to resolve... well, belongs to, ascendent, reference, many to one,  whatever you
  * call it kind of relation.
  *
  * @param $criterion
  * @param $sub_queries
  * @param $val
  * @return array
  */
 private function _run_ascendant_queries($criterion, $sub_queries, $val)
 {
     $has_error = false;
     $last_key = array_pop($sub_queries);
     $collections = [];
     $collections[] = $this->get_collection_name();
     $i = 1;
     foreach ($sub_queries as $attr) {
         $sub_collection = \Rocketr\Schema\Reference::get($collections[$i - 1], $attr);
         $collections[] = $sub_collection;
         $i++;
     }
     array_shift($collections);
     $references_ids = [];
     $d = count($collections);
     for ($d; $d > 0; $d--) {
         if ($d === count($sub_queries)) {
             $condition = $this->_attribute_criteria($criterion, $last_key, $val);
         } else {
             $condition = $this->_attribute_criteria('IN', $sub_queries[$d] . '.$id', $references_ids);
         }
         if ($collections[$d - 1]) {
             $ref_attr = \Rocketr\Schema\Reference::on($collections[$d - 1], $sub_queries[$d]);
             $result = $this->_run_sub_query($collections[$d - 1], $condition, $ref_attr);
             $references_ids = $this->_make_references_ids($result, $ref_attr);
             unset($result);
         } else {
             $has_error = true;
             break;
             //TODO : Exeption Missing reference declaration for implode('.', $sub_queries) . $criterion . $val;
         }
     }
     return !$has_error ? $references_ids : [];
 }