/** * @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; }
public function getResults() { $this->prepareResultsQuery(); return $this->_model->find_all()->as_array(); }
/** * 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; } }
/** * @param Model $item * @param array $relations * @return array */ protected function modelAsArrayWith($item, $relations) { $result = $item->as_array(); if (count($relations)) { foreach ($relations as $rel) { /** @var BaseModel $related */ $related = $item->{$rel}; $result[$rel] = $related->find_all()->as_array(true); } } return $result; }
public function values($row, $set_loaded = false) { parent::values($row, $set_loaded); $this->message = $this->_row['message']; return $this; }
/** * @inheritdoc */ public function __get($column) { if ($this->returnWrappers && in_array($column, $this->wrappers)) { return $this->wrappers[$column]; } return parent::__get($column); }
/** * Sets the user logged in via this provider. * The default implementation stores the users id * in a session variable. * * @param \PHPixie\ORM\Model $user Logged in user * @return void */ public function set_user($user) { $this->pixie->session->set($this->user_id_key, $user->id()); $this->service->set_user($user, $this->name); }
/** * 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(); }
/** * Regenerates users login token * * @param \PHPixie\ORM\Model $user User model to update * @return string Generated token */ public function regenerate_login_token($user) { $token_field = $this->login_token_field; $token = $this->random_string() . ':' . time(); $user->{$token_field} = $token; $user->save(); return $token; }
/** * @param \App\Model\Newsletter|\PHPixie\ORM\Model $newsletter */ protected function onSuccessfulEdit(Model $newsletter) { if ($this->request->post('send')) { $this->pixie->db->query('delete')->table('tbl_newsletter_instances')->where('newsletter_id', $newsletter->id())->execute(); $sql = "INSERT INTO tbl_newsletter_instances (newsletter_id, subscriber_id)\n" . "SELECT " . $newsletter->id() . ", ns.id\n" . "FROM tbl_newsletter_signups ns"; if (!$newsletter->send_to_all) { $subscriberIds = preg_split('/\\s*,\\s*/', $newsletter->subscriber_ids, -1, PREG_SPLIT_NO_EMPTY); if (!$subscriberIds) { $newsletter->save(); return; } $sql .= "\nWHERE ns.id IN (" . implode(', ', $subscriberIds) . ")"; } /** @var \PDO $conn */ $conn = $this->pixie->db->get()->conn; $conn->exec($sql); $newsletter->recipient_count = $newsletter->instances->count_all(); $newsletter->status = \App\Model\Newsletter::STATUS_SENDING; $newsletter->save(); } }
/** * Checks if the current generated token is still valid * * @param \PHPixie\ORM\Model $user User model to check * @return string Valid token */ public function get_valid_token($user) { if ($user instanceof \PHPixie\Auth\Login\Password\User) { $token = $user->login_token(); } else { $token_field = $this->login_token_field; $token = $user->{$token_field}; } $split_token = explode(':', $token); if (count($split_token) !== 2 || $split_token[1] < time() - $this->login_token_lifetime) { $split_token = explode(':', $this->regenerate_login_token($user)); } return $split_token[0]; }
public function save() { if ($this->loaded()) { $this->preUpdate(); } else { $this->preInsert(); } return parent::save(); }