Ejemplo n.º 1
0
 private function parseRelations()
 {
     $map = $this->getMap();
     $mapFields = $map->getFields();
     $relations = array_filter($mapFields, function ($elem) {
         return isset($elem['type']) && $elem['type'] == 'relation';
     });
     if (count($relations) > 0) {
         foreach ($relations as $field => $relation) {
             $relObjectType = $relation['itemType'];
             if (strpos($relObjectType, '\\') === false) {
                 $refClass = new \ReflectionClass($map->getObject());
                 $relObjectType = '\\' . $refClass->getNamespaceName() . "\\" . $relObjectType;
             }
             if (!class_exists($relObjectType)) {
                 throw new \Exception("Can't find relating type " . $relObjectType);
             }
             $relCollection = new DataObjectCollection(new $relObjectType());
             $relCollection->setMapper($this->mapper);
             $conds = array();
             foreach ($relation['conditions'] as $key => $fkey) {
                 if (isset($this->{$key})) {
                     $conds[$fkey] = $this->{$key};
                 } else {
                     throw new \Exception("Can't resolve relation ({$key} => {$fkey}) for " . get_class($this));
                 }
             }
             $relCollection->filter($conds);
             $this->{$field} = $relCollection;
         }
     }
 }