/** * Checks if the collection contains given instance of model by ID. * It is made, because PHPixie doesn't contain Unit of Work feature. * * @param Model $model * @return bool */ public function containsById(Model $model) { if ($model === null || !$model->loaded() || !$this->loaded()) { return false; } if (__CLASS__ !== get_class($model)) { return false; } $idField = $this->id_field; foreach ($this as $item) { if ($model->{$idField} == $item->{$idField}) { return true; } } return false; }
/** * @param Model $model PHPixie's model * @param null $rpcClassName GWT name of the entity * @return null|\IsSerializable */ public function transform(Model $model, $rpcClassName = null) { if ($model === null || !$model->loaded()) { return null; } if ($rpcClassName === null) { $rpcClassName = preg_replace('|^.*\\\\|', '', get_class($model)); } $fields = array_keys(get_class_vars($rpcClassName)); $columns = $model->columns(); $meta = self::loadMetaData($rpcClassName); $object = new $rpcClassName(); foreach ($fields as $field) { if (!in_array($field, $columns)) { continue; } $value = $model->__get($field); if (@$meta['fields'][$field]['type'] == 'Date') { $value = $value ? new \Date(strtotime($value)) : null; } $object->{$field} = $value; } return $object; }
/** * Move current node to a new position in the tree. * * @param \PHPixie\ORM\Model $parent Parent to append the node to. * @param bool $append_to_beginning Prepend node to the beginning of children list. * @param bool $children_only Whether to move children of the node instead of the whole node. * Defaults to false. * @return void */ protected function move_to($parent, $append_to_beginning = false, $children_only = false) { $width = $this->width(); if ($children_only) { $width = $width - 2; } if ($parent != null && $parent->loaded()) { $lpos = $append_to_beginning ? $parent->lpos + 1 : $parent->rpos; $depth = $parent->depth + 1; } else { $lpos = ($append_to_beginning ? 0 : $this->max_rpos_query()->execute()->current()->rpos) + 1; $depth = 0; } $rpos = $lpos + $width - 1; if ($this->model->loaded()) { $this->reverse_children_pos_query()->execute(); } $this->pad_rpos_query($lpos, $width)->execute(); $this->pad_lpos_query($lpos, $width)->execute(); if ($this->model->loaded()) { $this->collapse_rpos_query($width, $children_only)->execute(); $this->collapse_lpos_query($width)->execute(); $depth_offset = $depth - $this->model->depth; if ($lpos > $this->model->lpos) { $lpos = $lpos - $width; $rpos = $rpos - $width; } $pos_offset = $lpos - $this->model->lpos; if ($children_only) { $pos_offset = $pos_offset - 1; $depth_offset = $depth_offset - 1; } $this->update_reversed_query($pos_offset, $depth_offset)->execute(); } if (!$children_only) { $this->model->lpos = $lpos; $this->model->depth = $depth; $this->model->rpos = $rpos; } else { if ($lpos < $this->model->lpos) { $this->model->lpos = $this->model->lpos + $width; } $this->model->rpos = $this->model->lpos + 1; } }
/** * Removes a relationship between current item and the passed one * * @param string $relation Name of the relationship * @param \PHPixie\ORM\Model $model ORM item to remove relationship with. Can be omitted for 'belongs_to' relationships * @return void * @throws \Exception If realtionship is not defined * @throws \Exception If current item is not in the database yet (isn't considered loaded()) * @throws \Exception If passed item is not in the database yet (isn't considered loaded()) */ public function remove($relation, $model = null) { if (!$this->loaded()) { throw new \Exception("Model must be loaded before you try removing relationships from it."); } $rels = array_merge($this->has_one, $this->has_many, $this->belongs_to); $rel = $this->pixie->arr($rels, $relation, false); if (!$rel) { throw new \Exception("Model doesn't have a '{$relation}' relation defined"); } if ($rel['type'] != 'belongs_to' && (!$model || !$model->loaded())) { throw new \Exception("Model must be loaded before being removed from a has_one or has_many relationship."); } if ($rel['type'] == 'belongs_to') { $key = $rel['key']; $this->{$key} = null; $this->save(); } elseif (isset($rel['through'])) { $this->conn->query('delete')->table($rel['through'])->where(array(array($rel['key'], $this->_row[$this->id_field]), array($rel['foreign_key'], $model->_row[$model->id_field])))->execute(); } else { $key = $rel['key']; $model->{$key} = null; $model->save(); } $this->cached = array(); }