Exemple #1
0
 /**
  * List $_class relations
  * @return $this
  **/
 public function setRelations()
 {
     if (!property_exists($this->_class, 'relationships')) {
         return $this->_relations = [];
     }
     $class = $this->_class;
     foreach ($class::$relationships as $key => $relation) {
         $relation_object = (new $class())->{$relation}();
         $relation_class = join('', array_slice(explode('\\', get_class($relation_object)), -1));
         $this->_relations[$relation] = ['type' => join('', array_slice(explode('\\', $relation_class), -1)), 'model' => join('', array_slice(explode('\\', get_class($relation_object->getRelated())), -1)), 'relation_primary_key' => $relation_object->getRelated()->getKeyName(), 'relation_table' => $relation_object->getRelated()->getTable()];
         // Prevent infinite loops
         if ($this->_nest) {
             $relation_context = new Context(get_class($relation_object->getRelated()), null, false);
             $this->_relations[$relation]['relation_fields'] = $relation_context->getFields();
         }
         // Relation with pivot
         if (strtolower($relation_class) == 'belongstomany') {
             $this->_relations[$relation]['pivot_table'] = $relation_object->getTable();
             $this->_relations[$relation]['pivot_foreign_key'] = $relation_object->getForeignKey();
             $this->_relations[$relation]['pivot_other_key'] = $relation_object->getOtherKey();
             $pivot_context = new Context(get_class($relation_object->getRelated()), $relation_object->getTable(), false);
             $this->_relations[$relation]['pivot_fields'] = $pivot_context->getFields();
         } else {
             $this->_relations[$relation]['foreign_key'] = $relation_object->getForeignKey();
         }
     }
 }