Exemplo n.º 1
0
 function findEntity(Metadata $md, $field)
 {
     $otherClass = null;
     $join = null;
     if ($md->hasFKey($field)) {
         $class = $md->getFKey($field);
         $other = Store::me()->createAndAddClass($class);
         if (is_null($other)) {
             $other = Store::me()->get($class);
         }
         $join = new JoinFKey();
         $join->setFrom($md);
         $join->setTo($other);
     } else {
         if ($md->hasRKey($field)) {
             $class = $md->getRKey($field);
             $other = Store::me()->createAndAddClass($class);
             if (is_null($other)) {
                 $other = Store::me()->get($class);
             }
             $join = new JoinRKey();
             $join->setTo($md);
             $join->setFrom($other);
         } else {
             throw new \Exception("{$field} throws an error");
             //Notes::show( Store::me() );
         }
     }
     $join->setAlias($field);
     return $join;
 }
Exemplo n.º 2
0
 function load($class)
 {
     if (!s::me()->hasClass($class)) {
         s::me()->addClass(s::me()->createClassByName($class));
     }
     $this->main = s::me()->get($class);
     $this->fields = $this->main->mapFields($this->main->getInstance()->alias);
     $this->getFilter()->clear();
     $this->joins = array();
     $this->recordset = array();
     $this->collection = array();
     return $this;
 }
Exemplo n.º 3
0
 private function joinCascade(Entity $e, &$source, $join, $current)
 {
     $md = $e->getMetadata();
     if ($md->hasFKey($join->alias)) {
         $class = $md->getFKey($join->alias);
         $joinType = 'fk';
     } else {
         if ($md->hasRKey($join->alias)) {
             $class = $md->getRKey($join->alias);
             $joinType = 'rk';
         } else {
             $joinType = 'fk';
         }
     }
     switch ($joinType) {
         case 'fk':
             $id = $source["{$join->alias}_id"];
             $obj = $this->map[$join->alias][$id];
             $nextEnt = Store::me()->get($class)->cloneIt($id);
             $nextEnt->setAlias($join->alias)->loadValues($obj);
             if (isset($join->next)) {
                 $this->joinCascade($nextEnt, $obj, $join->next, $join->alias);
             }
             $method = $md->getSetter($join->alias);
             $e->{$method}($nextEnt);
             $source[$join->alias] = $obj;
             break;
         case 'rk':
             $obj = array_filter($this->map[$join->alias], function ($item) use($source, $current) {
                 return $item["{$current}_id"] == $source['id'];
             });
             if (isset($join->next)) {
                 if (version_compare(phpversion(), '5.4.14', '<=')) {
                     foreach ($obj as $item) {
                         $nextEnt = Store::me()->get($class)->cloneIt($item["id"]);
                         $nextEnt->setAlias($join->alias)->loadValues($item);
                         $this->joinCascade($nextEnt, $item, $join->next, $join->alias);
                         $method = $md->getAdder($join->alias);
                         $e->{$method}($nextEnt);
                     }
                 } else {
                     array_walk($obj, function (&$item) use($join, $class, $md, &$nextId, &$e) {
                         $nextEnt = Store::me()->get($class)->cloneIt($item["id"]);
                         $nextEnt->setAlias($join->alias)->loadValues($item);
                         //$this->joinCascade( $nextEnt, $item, $join->next, $join->alias );
                         $this->joinCascade($nextEnt, $item, $join->next, $nextEnt->table);
                         $method = $md->getAdder($join->alias);
                         $e->{$method}($nextEnt);
                     });
                 }
             } else {
                 //$rkeyObj = current( $obj );
                 foreach ($obj as $rkeyObj) {
                     if (isset($rkeyObj['id'])) {
                         $nextEnt = Store::me()->get($class)->cloneIt($rkeyObj['id']);
                         $nextEnt->setAlias($join->alias)->loadValues($rkeyObj);
                         $method = $md->getAdder($join->alias);
                         $e->{$method}($nextEnt);
                         $source[$join->alias][$rkeyObj['id']] = $rkeyObj;
                     }
                 }
             }
             $source[$join->alias] = $obj;
             break;
     }
 }
Exemplo n.º 4
0
 private function getFieldsToSave()
 {
     //$fields = s::me()->getMetadata($this->class)->getFields();
     $fields = $this->getMetadata()->getFields();
     unset($fields['id']);
     $keys = s::me()->getMetadata($this->class)->getAllFKeys();
     array_walk($keys, function (&$item, &$key) {
         $item = "{$key}_id";
     });
     return array_merge($fields, $keys);
 }
Exemplo n.º 5
0
 function parse(Metadata $main, $path)
 {
     $ex = explode("->", $path);
     $md = $main;
     $j = null;
     $j1 = null;
     foreach ($ex as $piece) {
         if (is_null($j)) {
             $j = new stdClass();
             $j->alias = $piece;
             $j->class = $md->hasFKey($piece) ? $md->getFKey($piece) : $md->getRKey($piece);
             $j1 = $j;
             $md = Store::me()->get($j->class);
             continue;
         }
         $current = new stdClass();
         $current->alias = $piece;
         $current->source = $j1->alias;
         if ($md->hasFKey($piece)) {
             $current->class = $md->getFKey($piece);
         } else {
             if ($md->hasRKey($piece)) {
                 $current->class = $md->getRKey($piece);
             }
         }
         $md = Store::me()->get($current->class);
         $j1->next = $current;
         $j1 = $j1->next;
     }
     return $j;
 }