protected final function belongsTo(ModelRelation $relation) { // checking options from relations $opts = $relation->getOptions(); if (!isset($opts["alias"])) { return; } // build local needed variable $field = $relation->getFields(); $reference = $relation->getReferencedFields(); $modelRelation = $relation->getReferencedModel(); // check if related field exist or not if (!isset($this->data[$field])) { return; } // build data.links $this->data["links"][$reference] = (int) $this->data[$field]; // check if data[related] already populated if (in_array($this->data[$field], $this->belongsToIds)) { return; } // store to haystack $this->belongsToIds[] = $this->data[$field]; $query = new Query(); $query->addCondition(new SimpleCondition($reference, Operator::OP_EQUALS, $this->data[$field])); // fetch model data, otherwise throw an exception try { $handler = new Crud(); /** @var $relationModel NgModelBase */ $relationModel = $handler->read(new $modelRelation(), $query, true); unset($handler); } catch (CrudException $e) { throw new Exception($e->getMessage()); } // check if the model was an instance of NgModel if (!$relationModel instanceof NgModelBase) { return; } // envelope relationModel to get relation data $relationData = $this->envelope->envelope($relationModel); // check if linked[reference] already populated if (!isset($this->linked[$reference])) { $this->linked[$reference] = array(); } // put relation data on linked $this->linked[$reference][] = $relationData; // remove data[field] unset($this->data[$field]); }
/** * load the query object for a hasManyToMany relationship * build most of the joins manually since by reference relationship in the model isn't working so well * this support joins to distant tables with parent models * * @param Relation|PhalconRelation $relation * @return array */ protected function getHasManyToManyRecords($relation) { $refModelNameSpace = $relation->getReferencedModel(); $intermediateModelNameSpace = $relation->getIntermediateModel(); // determine the key to search against $field = $relation->getFields(); $referencedField = $relation->getReferencedFields(); $intermediateFields = $relation->getIntermediateReferencedFields(); $config = $this->getDI()->get('config'); $modelNameSpace = $config['namespaces']['models']; $mm = $this->getDI()->get('modelsManager'); /** @var Builder $query */ $query = $mm->createBuilder()->from($intermediateModelNameSpace)->join($refModelNameSpace, $refModelNameSpace . ".{$referencedField} = " . $intermediateModelNameSpace . ".{$intermediateFields}"); $columns = array(); // join in parent record if one is detected $parentName = $relation->getParent(); if ($parentName) { // load parent model /** @var BaseModel $parentModel */ $parentModelNameSpace = $modelNameSpace . $parentName; $parentModel = new $refModelNameSpace(); // load reference relationship $parentRelationship = $parentModel->getRelation($parentName); $columns[] = "{$parentModelNameSpace}.*"; $query->join($parentModelNameSpace, "{$parentModelNameSpace}." . $parentRelationship->getReferencedFields() . " = {$refModelNameSpace}." . $parentRelationship->getFields(), $parentModelNameSpace); } // Load the main record field at the end, so they are not overwritten $columns[] = $refModelNameSpace . ".*, " . $intermediateModelNameSpace . ".*"; $query->columns($columns); if (isset($this->baseRecord[$field])) { $fieldValue = $this->baseRecord[$field]; } else { // fall back to using the primaryKeyValue $fieldValue = $this->primaryKeyValue; } $whereField = $intermediateModelNameSpace . '.' . $relation->getIntermediateFields(); $query->where("{$whereField} = \"{$fieldValue}\""); $result = $query->getQuery()->execute(); return $this->loadRelationRecords($result, $relation); }