Ejemplo n.º 1
0
 /**
  * Init list contents based on given guide of variable length property-value 
  * pairs for query constructor.
  * See documentation for details.
  * 
  * @return this
  */
 public function init()
 {
     $guide = \deco\essentials\util\Arguments::pvToArray(func_get_args());
     $cls = $this->instance;
     $table = $cls::getTable();
     $query = self::db()->fluent()->from($table)->select(null);
     if (array_key_exists('recursion', $guide)) {
         $query = self::createQueryRecursively($cls, $guide['recursion'], $query);
     } else {
         $columns = $cls::getDatabaseHardColumnNames();
         $query = $query->select(self::getSelectInJoin($table, $columns));
     }
     if (array_key_exists('where', $guide)) {
         $query = $query->where($guide['where']);
     }
     if (array_key_exists('orderBy', $guide)) {
         $query = $query->orderBy($guide['orderBy']);
     } else {
         // default sort is by container columns
         $sort = $cls::getDatabaseSortColumns();
         if (count($sort)) {
             $query = $query->orderBy($sort);
         }
     }
     if (array_key_exists('limit', $guide)) {
         $query = $query->limit($guide['limit']);
     }
     if (array_key_exists('groupBy', $guide)) {
         $query = $query->groupBy($guide['groupBy']);
     }
     $data = self::db()->getAsArray($query->execute());
     foreach ($data as $row) {
         $id = $row[$table . '_id'];
         if (!array_key_exists($id, $this->objects)) {
             array_push($this->sort, $id);
             $objectData = self::getDataFromSelectFor($table, $row);
             $this->objects[$id] = $cls::initFromRow($objectData);
         }
         if (array_key_exists('recursion', $guide)) {
             self::initRecursively($this->objects[$id], $guide['recursion'], $row);
         }
     }
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * @call $obj->load{Property}(), $obj->load($property, property value pairs of guide for initialization)
  * @return instance of inited object (could be also value if query is value)   
  */
 protected function DECOloadProperty()
 {
     $args = func_get_args();
     $property = $args[0];
     if (count($query = self::getPropertyAnnotationValue($property, 'query', array())) > 0) {
         if (array_key_exists('table', $query)) {
             $q = self::db()->fluent()->from($query['table'])->select(null)->select($query['columns']);
             if (array_key_exists('where', $query)) {
                 foreach ($query['where'] as $key => $value) {
                     if (preg_match('#^{([A-Za-z]*)}$#', $value, $matches)) {
                         $query['where'][$key] = $this->master()->get($matches[1]);
                     }
                 }
                 $q = $q->where($query['where']);
             }
             if (array_key_exists('orderBy', $query)) {
                 $q = $q->orderBy($query['orderBy']);
             }
             if (array_key_exists('limit', $query)) {
                 $q = $q->limit($query['limit']);
             }
             $data = self::db()->get($q->execute());
         } else {
             while (preg_match('#{([A-Za-z]*)}#', $query[0], $matches)) {
                 $query[0] = preg_replace('#{' . $matches[1] . '}#', $this->master()->get($matches[1]), $query[0]);
             }
             $data = self::db()->get($query[0]);
         }
         $type = self::getPropertyAnnotationValue($property, 'type', false);
         if ($type == 'array') {
             $this->{$property} = $data;
         } else {
             $this->{$property} = array_pop($data);
             if ($type !== false) {
                 settype($this->{$property}, $type);
             }
         }
         return $this->{$property};
     }
     array_shift($args);
     if (count($args) == 0) {
         $args[0] = array();
     }
     $guide = is_array($args[0]) ? $args[0] : \deco\essentials\util\Arguments::pvToArray($args);
     $cls = self::getPropertyAnnotationValue($property, 'contains');
     $annCol = self::getMasterAnnotationCollection();
     $masterCls = $annCol->getValue('contains');
     $masterProperty = $annCol->reflector->name;
     $isCollection = $cls::isListOfService();
     if (!$isCollection && !self::getPropertyAnnotationValue($property, 'parent', false)) {
         $foreign = $masterCls::getReferenceToClass($cls);
         $masterValue = $this->{$masterProperty}->get($foreign['column']);
         $this->{$property} = new $cls($masterValue);
     } else {
         $foreign = $cls::getReferenceToClass($masterCls);
         $masterValue = $this->{$masterProperty}->get($foreign['parentColumn']);
         if (self::getPropertyAnnotationValue($property, 'parent', false)) {
             $this->{$property} = new $cls($foreign['column'], $masterValue);
         } else {
             $this->{$property} = new $cls();
             $ar = array('where', array($foreign['column'] => $masterValue));
             if (!is_null($guide)) {
                 $ar = array_merge($ar, $guide);
             }
             call_user_func_array(array($this->{$property}, 'init'), $ar);
         }
     }
     return $this->{$property};
 }