Example #1
0
 /**
  * 返回当前节点的后代
  *
  * @param  bool   $self               是否包含当前节点
  * @param  string $direction          排序
  * @param  bool   $directChildrenOnly 只包含相邻的下级节点
  * @param  bool   $leavesOnly         只包含叶子节点
  * @param  int    $limit              要获取的记录条数
  * @return MPTT[]
  */
 public function descendants($self = false, $direction = 'ASC', $directChildrenOnly = false, $leavesOnly = false, $limit = 0)
 {
     $left_operator = $self ? '>=' : '>';
     $right_operator = $self ? '<=' : '<';
     $query = new self();
     $query->where($this->_leftColumn, $left_operator, $this->left())->where($this->_rightColumn, $right_operator, $this->right())->where($this->_scopeColumn, '=', $this->scope())->orderBy($this->_leftColumn, $direction);
     if ($directChildrenOnly) {
         if ($self) {
             $query->andWhereOpen()->where($this->_levelColumn, '=', $this->level())->orWhere($this->_levelColumn, '=', $this->level() + 1)->andWhereClose();
         } else {
             $query->where($this->_levelColumn, '=', $this->level() + 1);
         }
     }
     if ($leavesOnly) {
         $query->where($this->_rightColumn, '=', $this->_leftColumn . ' + 1');
     }
     if ($limit) {
         $query->limit($limit);
     }
     return $query->findAll();
 }
Example #2
0
 public static function factory($conditions)
 {
     if ($conditions instanceof BaseClass) {
         return $conditions;
     }
     $select = new self();
     if (is_array($conditions) || $conditions instanceof \Traversable) {
         foreach ($conditions as $name => $condition) {
             switch ($name) {
                 case 'columns':
                     if (is_array($condition) && array_key_exists(0, $condition) && is_array($condition[0])) {
                         call_user_func_array(array($select, 'columns'), $condition);
                         continue;
                     }
                     $select->columns($condition);
                     break;
                 case 'combine':
                     if (is_array($condition)) {
                         call_user_func_array(array($select, 'combine'), $condition);
                         continue;
                     }
                     $select->combine($condition);
                     break;
                 case 'from':
                     $select->from($condition);
                     break;
                 case 'group':
                     $select->group($condition);
                     break;
                 case 'having':
                     if (is_array($condition) && array_key_exists(1, $condition) && ($condition[1] == PredicateSet::OP_AND || $condition[1] == PredicateSet::OP_OR)) {
                         call_user_func_array(array($select, 'having'), $condition);
                         continue;
                     }
                     $select->having($condition);
                     break;
                 case 'join':
                     call_user_func_array(array($select, 'join'), $condition);
                     break;
                 case 'limit':
                     $select->limit($condition);
                     break;
                 case 'offset':
                     $select->offset($condition);
                     break;
                 case 'order':
                     $select->order($condition);
                     break;
                 case 'where':
                     if (is_array($condition) && array_key_exists(1, $condition) && ($condition[1] == PredicateSet::OP_AND || $condition[1] == PredicateSet::OP_OR)) {
                         call_user_func_array(array($select, 'where'), $condition);
                         continue;
                     }
                     $select->where($condition);
                     break;
                 default:
                     throw new \Exception("Invalid condition ({$name})");
             }
         }
         return $select;
     }
     throw new \Exception(sprintf('Invalid conditions type (%s)', is_object($conditions) ? get_class($conditions) : gettype($conditions)));
 }
Example #3
0
 /**
  * we get the locations in an array miltidimensional by deep.
  * @return array 
  */
 public static function get_multidimensional($limit = NULL)
 {
     $cache_name = is_int($limit) ? 'locs_m' . '_' . $limit : 'locs_m';
     if (($locs_m = Core::cache($cache_name)) === NULL) {
         $locs = new self();
         $locs->order_by('order', 'asc');
         if (is_int($limit)) {
             $locs->limit($limit);
         }
         $locs = $locs->find_all()->cached()->as_array('id_location');
         //for each location we get his siblings
         $locs_s = array();
         foreach ($locs as $loc) {
             $locs_s[$loc->id_location_parent][] = $loc->id_location;
         }
         //last build multidimensional array
         if (count($locs_s) > 1) {
             $locs_m = self::multi_locs($locs_s);
         } else {
             $locs_m = array();
         }
         Core::cache($cache_name, $locs_m);
     }
     return $locs_m;
 }
Example #4
0
 /**
  * we get the categories in an array miltidimensional by deep.
  * @return array 
  */
 public static function get_multidimensional($limit = NULL)
 {
     $cache_name = is_int($limit) ? 'cats_m' . '_' . $limit : 'cats_m';
     //multidimensional array
     if (($cats_m = Core::cache($cache_name)) === NULL) {
         $cats = new self();
         $cats->order_by('order', 'asc');
         if (is_int($limit)) {
             $cats->limit($limit);
         }
         $cats = $cats->find_all()->cached()->as_array('id_category');
         //for each category we get his siblings
         $cats_s = array();
         foreach ($cats as $cat) {
             $cats_s[$cat->id_category_parent][] = $cat->id_category;
         }
         //last build multidimensional array
         if (count($cats_s) > 1) {
             $cats_m = self::multi_cats($cats_s);
         } else {
             $cats_m = array();
         }
         Core::cache($cache_name, $cats_m);
     }
     return $cats_m;
 }