Esempio n. 1
0
 public function eager_load($target)
 {
     $cache = WaxModel::get_cache($this->target_model . ":" . md5(serialize($target->filters)), $this->field, $this->model->primval, false);
     if (is_array($cache)) {
         return new WaxModelAssociation($this->model, $target, $cache, $this->field);
     }
     $vals = $target->filter(array($this->join_field => $this->model->primval))->all();
     WaxModel::set_cache($this->target_model . ":" . md5(serialize($target->filters)), $this->field, $this->model->primval, $vals->rowset);
     return new WaxModelAssociation($this->model, $target, $vals->rowset, $this->field);
 }
Esempio n. 2
0
 public function get()
 {
     $class = $this->target_model;
     if ($cache = WaxModel::get_cache($class, $this->field, $this->model->primval)) {
         return $cache;
     }
     $model = new $this->target_model($this->model->{$this->col_name});
     if ($model->primval) {
         WaxModel::set_cache($class, $this->field, $this->model->primval, $model);
         return $model;
     } else {
         return false;
     }
 }
Esempio n. 3
0
 public function cache_whole_tree()
 {
     $class = get_class($this);
     $all_nodes = $this->all();
     //index the rows by their ids, as well as parents and children ids
     $indexed_rowset = array();
     foreach ($all_nodes->rowset as $row) {
         if (!$indexed_rowset[$row['id']]['children']) {
             $indexed_rowset[$row['id']]['children'] = array();
         }
         //cache empty children arrays too
         $indexed_rowset[$row['id']]['row'] = $row;
         $indexed_rowset[$row['id']]['parent_id'] = $row['parent_id'];
         $indexed_rowset[$row['parent_id']]['children'][] = $row;
     }
     foreach ($indexed_rowset as $id => $entry) {
         //set parent cache
         $parent = new $class();
         $parent->set_attributes($indexed_rowset[$entry['parent_id']]['row']);
         WaxModel::set_cache($class, $this->parent_column, $id, $parent);
         //set children cache
         WaxModel::set_cache($class, $this->children_column, $id, $entry['children']);
     }
 }
Esempio n. 4
0
 private function lazy_load($target_model)
 {
     $left_field = $this->model->table . "_" . $this->model->primary_key;
     $right_field = $target_model->table . "_" . $target_model->primary_key;
     $this->join_model->select_columns = $right_field;
     $ids = array();
     if ($cache = WaxModel::get_cache(get_class($this->model), $this->field, $this->model->primval . ":" . md5(serialize($target_model->filters)), false)) {
         return new WaxModelAssociation($this->model, $target_model, $cache, $this->field);
     }
     foreach ($this->join_model->rows() as $row) {
         $ids[] = $row[$right_field];
     }
     WaxModel::set_cache(get_class($this->model), $this->field, $this->model->primval . ":" . md5(serialize($target_model->filters)), $ids);
     return new WaxModelAssociation($this->model, $target_model, $ids, $this->field);
 }