/** * Shortcut method to register a sub collection in knot::map::collection * * @param $attribute_s * @param $target_s * @param array|null $on * @return $this */ public function has_collection($attribute_s, $target_s, $on = null) { if ($this->_collection_name) { \Rocketr\Schema\HasCollection::add($this->_collection_name, [$attribute_s => $target_s], $on); } return $this; }
/** * 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; } }
/** * @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); } } }
private function _run_hascollection_queries($criterion, $sub_queries, $val) { $ref_attr = array_shift($sub_queries); $target = \Rocketr\Schema\HasCollection::get($this->get_collection_name(), $ref_attr); $condition = $this->_stringify_condition($criterion, $sub_queries, $val); $on = \Rocketr\Schema\Map::on($this->get_collection_name(), $ref_attr); $result = $this->_run_sub_query($target, $condition, $on); return $this->_make_references_ids($result, $on, $ref_attr); }