public function before_update(\Orm\Model $obj) { if (!$obj->is_changed('comment_count') && !$obj->is_changed('like_count')) { return; } $obj->importance_level = \Timeline\Site_Util::get_importance_level($obj->comment_count, $obj->like_count); }
public function before_save(\Orm\Model $obj) { $timestamp = \DB::expr('CURRENT_TIMESTAMP'); if ($obj->is_new()) { $obj->created_at = $timestamp; } $obj->updated_at = $timestamp; }
/** * Sets the sort property to the current sort value * * @param Model Model object subject of this observer method */ public function before_insert(Orm\Model $obj) { if ($obj instanceof SortableInterface) { $max = $obj->getSortMax(); } else { $max = $obj->query()->max($this->_property); } $obj->{$this->_property} = $max + $this->_offset; }
/** * @param \ORM\Model $record * @return bool|mixed */ function validate(\ORM\Model $record) { $class_name = $record::className(); if ($record->isPersisted()) { return count($class_name::where($this->field . " = ? AND " . $class_name::getPrimaryKey() . " != ?", [$record->{$this->field}, $record->{$class_name::getPrimaryKey()}])) === 0; } else { return count($class_name::where($this->field . " = ?", [$record->{$this->field}])) === 0; } }
public function before_save(\Orm\Model $obj) { if ($this->_overwrite or empty($obj->{$this->_property})) { if ($obj->is_new()) { $obj->{$this->_property} = $obj->{$this->_property_from}; } else { $obj->{$this->_property} = $this->_mysql_timestamp ? \Date::time()->format('mysql') : \Date::time()->get_timestamp(); } } }
/** * Sets the CreatedBy property to the current user id * * @param Model Model object subject of this observer method */ public function before_insert(Orm\Model $obj) { if ($obj instanceof Orm\Model_Temporal) { if ($obj->{$obj->temporal_property('end_column')} !== $obj->temporal_property('max_timestamp')) { return false; } } if ($user_id = \Auth::get_user_id()) { $obj->{$this->_property} = $user_id[1]; } }
/** * Reads a record * * @param mixed $pk Primary key of the record * @return Model|bool Returns a Model or returns false if there is no match */ public function read($pk) { $model = new Model($this->modelName); $pkName = $model->getPK()['name']; $method = sprintf('filterBy%s', $pkName); $result = $model->{$method}($pk, Model::OPERATOR_EQUAL)->limit(1)->find(); if (is_array($result)) { # empty array if (sizeof($result) < 1) { return false; } } else { return false; } return $result[0]; }
public function before_save(\Orm\Model $model) { $groupName = $model->{$this->_property}; $diff = $model->get_diff(); if (!key_exists($groupName, $diff[0])) { return; } echo '<pre>'; print_r($model->get_diff()); exit; $modelClass = get_class($model); $existing = $modelClass::find('all', array('where' => array(array('name', $groupName)))); if ($existing) { throw new ColumnNotUnique(\Lang::get('ethanol.errors.alreadyDefined', array('value' => $groupName, 'property' => $this->_property))); } }
/** * Saves or updates the model form * * @return mixed Returns the primary key of the new/updated record or * returns false if the form data is not valid */ public function save() { if ($this->isValid()) { $fields = $this->model->modelFields(); foreach ($fields as $name => $field) { if (isset($this->fields->{$name})) { if (!$field->getAuto()) { $method = sprintf('set%s', $name); $label = sprintf('%s_%s', Field::FIELD_NAME_PREFIX, $name); if (get_class($field) == 'ORM\\Field\\IntegerField') { $this->model->{$method}(intval($this->fields->{$name}->getValue())); } elseif (get_class($field) == ModelField::class) { $getMethod = sprintf('get%s', $name); $pk = $this->model->{$getMethod}()->getPK(); $setMethod = sprintf('set%s', $pk['name']); $this->model->{$getMethod}()->{$setMethod}(intval($this->fields->{$name}->getValue())); } else { $this->model->{$method}($this->fields->{$name}->getValue()); } } } } return $this->model->save(); } return false; }
public function after_save(\Orm\Model $obj) { $save_data = array(); foreach (array_keys($obj->properties()) as $p) { $save_data[$p] = $obj->{$p}; } $host = empty(self::$td_config['host']) ? null : self::$td_config['host']; $port = empty(self::$td_config['port']) ? null : self::$td_config['port']; $options = empty(self::$td_config['options']) ? array() : self::$td_config['options']; $packer = empty(self::$td_config['packer']) ? null : self::$td_config['packer']; $database = empty(self::$td_config['database']) ? 'default' : self::$td_config['database']; $table_name = $obj->table(); \Fluent\Autoloader::register(); $logger = new \Fluent\Logger\FluentLogger($host, $port, $options, $packer); $res = $logger->post('td.' . $database . '.' . $table_name, $save_data); }
public static function find($id = null, array $options = array(), $dialler = null) { if (!is_null($dialler)) { self::$_connection = $dialler; } return parent::find($id, $options); }
public function __isset($name) { switch ($name) { case 'fullname': return isset($this->fname) && isset($this->mname) && isset($this->lname); default: return parent::__isset($name); } }
public function delete($cascade = null, $use_transation = false) { $ports = $this->get('ports'); foreach ($ports as $port) { $port->vlan = 0; $port->save(); } parent::delete(); }
public function &__get($field) { switch ($field) { case 'isWholeday': $retVal = floor($this->date / 86400) == floor($this->end_date / 86400) && $this->date % 86400 == 0 && $this->end_date % 86400 == 86399; return $retVal; default: return parent::__get($field); } }
public function &__get($property) { switch ($property) { case 'name': $name = $this->name = $this->subject_desc . ' (' . $this->subject_code . ')'; return $name; default: return parent::__get($property); } }
public function delete($cascade = null, $use_transation = false) { $id = $this->get('id'); //remove images from disk //path to 1000px image $this->unlink_img(DOCROOT . 'images/temp' . $id . '.png'); //path to 135px image $this->unlink_img(DOCROOT . 'images/tumb/temp' . $id . '.png'); //delete from database parent::delete(); }
public function after_update(\Orm\Model $obj) { if (!($timeline_caches = \Timeline\Model_TimelineCache::get4timeline_id($obj->id))) { return; } foreach ($timeline_caches as $timeline_cache) { // is_follow record のみ timeline_cache.id の付け直し if ($obj->is_changed('sort_datetime') && $timeline_cache->is_follow) { $timeline_cache->delete(); $timeline_cache->save(); } if ($obj->is_changed('public_flag')) { $timeline_cache->public_flag = $obj->public_flag; } if ($obj->is_changed('comment_count')) { $timeline_cache->comment_count = $obj->comment_count; } if ($obj->is_changed('like_count')) { $timeline_cache->like_count = $obj->like_count; } if ($obj->is_changed('importance_level')) { $timeline_cache->importance_level = $obj->importance_level; } $timeline_cache->save(); } }
public static function set_form_fields($form, $instance = null) { // Call parent for create the fieldset and set default value parent::set_form_fields($form, $instance); // Set authors foreach (\Model_User::find('all') as $user) { $form->field('user_id')->set_options($user->id, $user->username); } // Set categories foreach (\Model_Category::find('all') as $category) { $form->field('category_id')->set_options($category->id, $category->name); } }
public function &__get($key) { switch ($key) { case 'coursename': $tmp = $this->course_name; return $tmp; case 'slug': $tmp = preg_replace('/\\W+/', '_', strtolower($this->course_name)); return $tmp; default: return parent::__get($key); } }
public function __construct(array $data = array(), $new = true) { // call the ORM model constructor parent::__construct($data, $new); // process the model's tree properties, set some defaults if needed if (isset(static::$tree) and is_array(static::$tree)) { foreach (static::$defaults as $key => $value) { $this->configuration[$key] = isset(static::$tree[$key]) ? static::$tree[$key] : static::$defaults[$key]; } } else { $this->configuration = array_merge(static::$defaults, $this->configuration); } // array of read-only column names foreach (array('left_field', 'right_field', 'tree_field', 'symlink_field') as $field) { !empty($this->configuration[$field]) and $this->readonly_fields[] = $this->configuration[$field]; } if (count(static::$_primary_key) > 1) { throw new \Exception('The NestedSets model doesn\'t support ORM Models with multiple primary key columns.'); } }
/** * Generates and executes the search action * * @return array|bool Returns an array of Model classes or returns false * if there is no match */ public function find() { $this->queryAction = 'SELECT'; $query = $this->buildQuery(); $result = $this->queryFetch($query); if ($result) { $dataset = array(); foreach ($result as $row) { $model = new Model($this->name, false); foreach ($row as $column => $value) { $modelPath = explode(self::TABLE_NAME_SEPARATOR, $column); $modelPathLen = sizeof($modelPath); if ($modelPath[0] == $model->modelName()) { if ($modelPath[0] == $model->modelName() && $modelPathLen == 2) { $method = sprintf('set%s', $modelPath[1]); $model->{$method}($value); } else { $childModel = $model; for ($i = 1; $i < $modelPathLen - 1; $i++) { $method = sprintf('get%s', $modelPath[$i]); $childModel = $childModel->{$method}(); } $method = sprintf('set%s', $modelPath[$modelPathLen - 1]); $childModel->{$method}($value); } } } $dataset[] = $model; } return $dataset; } return false; }
/** * Checks to see if any models in the given relation are changed * This function is lazy so will return true as soon * as it finds something that has changed * * @param Model $obj * @param string $relation * * @return boolean */ protected function relation_changed(Orm\Model $obj, $relation) { // Check that the relation exists if ($obj->relations($relation) === false) { throw new \InvalidArgumentException('Unknown relation ' . $relation); } // If the relation is not loaded then ignore it. if (!$obj->is_fetched($relation)) { return false; } $relation_object = $obj->relations($relation); // Check if whe have a singular relation if ($relation_object->is_singular()) { // If so check that one model return $obj->{$relation}->is_changed(); } // Else we have an array of related objects so start checking them all foreach ($obj->{$relation} as $related_model) { if ($related_model->is_changed()) { return true; } } return false; }
function setUp() { $connection = new \ORM\Connection(DB_ADAPTER, DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASS); \ORM\Model::establishConnection($connection); parent::setUp(); }
public static function check_properties_updated_without_ignores(\Orm\Model $obj, $ignore_properties) { if (empty($ignore_properties)) { return false; } $ignore_properties = (array) $ignore_properties; $all_properties = \Util_Db::get_columns($obj::get_table_name()); foreach ($all_properties as $property) { if (in_array($property, $ignore_properties)) { continue; } if ($obj->is_changed($property)) { return true; } } return false; }
function testTransactions() { $this->assertCount(3, User::all()); \ORM\Model::beginTransaction(); $this->createNewUser(); \ORM\Model::rollback(); $this->assertCount(3, User::all()); \ORM\Model::beginTransaction(); $this->assertTrue(\ORM\Model::inTransaction()); $this->createNewUser(); \ORM\Model::commit(); $this->assertCount(4, User::all()); }
public function before_save(\Orm\Model $obj) { if ($obj->is_new() or $obj->is_changed()) { $obj->{$this->_property} = \Input::real_ip(); } }
/** * Delete relations * * @param int|\ORM\Model $payload * @return bool */ function delete($payload = null) { $id = null; if ($payload instanceof Model && isset($payload->{$payload::getPrimaryKey()}) && is_numeric($payload->{$payload::getPrimaryKey()})) { $id = $payload->{$payload::getPrimaryKey()}; } else { if (is_numeric($payload)) { $id = $payload; } } $model = $this->model; $pKey = $model::getPrimaryKey(); if (is_null($id)) { // remove all linked rows $sql = "DELETE FROM {$this->tableName} WHERE " . $this->getForeignKey() . ' = ?'; return Model::getConnection()->runQuery($sql, [$this->model->{$pKey}]); } else { return $this->pivot()->findOne([$this->getForeignKey(), $this->getForeignKeyRelated()], [$this->model->{$pKey}, $id])->destroy(); } }
/** * Gets a user's sign in ip address, handling int to ip conversion * * @param string $column * * @return string|int */ protected function get_sign_in_ip($column) { $ip = parent::__get($column); $value = $ip != 0 ? long2ip($ip) : 0; return $value; }
public function __construct(array $data = array(), $new = true, $view = null, $cache = true) { parent::__construct($data, $new, $view, $cache); $this->app_admin_path = APPPATH . 'classes' . DS . 'controller' . DS . 'admin' . DS; }
public function before_save(\Orm\Model $obj) { $latlng = $obj->{$this->_property}; if (!$latlng) { return; } $obj->set($this->_property, \DB::expr('GeomFromText("POINT(' . $latlng[0] . ' ' . $latlng[1] . ')")')); }