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); }
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; } }
/** * get the root nodes * now with caching! yey! * @return WaxRecordSet of all the self-parented nodes or nodes with unidentifiable parents */ public function roots() { if ($root_return = WaxModel::get_cache($this->table, "parent", "rootnodes")) { return $root_return; } /** Methods of finding a root node **/ //First method: parent reference same as primary key $filter[] = "{$this->parent_column}_{$this->primary_key} = {$this->primary_key}"; //Second method: parent references a non-existant node (including 0) $filter[] = "{$this->parent_column}_{$this->primary_key} NOT IN (SELECT {$this->primary_key} FROM `{$this->table}`)"; //Third method: parent references a nothing $filter[] = "{$this->parent_column}_{$this->primary_key} IS NULL OR {$this->parent_column}_{$this->primary_key} = 0"; $root_return = $this->filter("(" . join(" OR ", $filter) . ")")->order('id')->all(); if ($root_return) { return $root_return; } else { return false; } }
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); }