/** * Get the value of the field of a loaded model. If model is not loaded * will return default value instead. * * @return mixed current value of a field */ public function get() { if ($this->owner->loaded() || isset($this->owner->data[$this->short_name])) { return $this->owner->get($this->short_name); } return $this->defaultValue(); }
/** * ref() will traverse reference and will attempt to load related model's entry. If the entry will fail to load * it will return model which would not be loaded. This can be changed by specifying an argument:. * * 'model' - simply create new model and return it without loading anything * false or 'ignore' - will not even try to load anything * null (default) - will tryLoad() * 'load' - will always load the model and if record is not present, will fail * 'create' - if record fails to load, will create new record, save, get ID and insert into $this * 'link' - if record fails to load, will return new record, with appropriate afterSave hander, which will * update current model also and save it too. * * @param string|bool|null $mode * * @return Model */ public function ref($mode = null) { if ($mode == 'model') { return $this->add($this->model_name); } $this->getModel()->unload(); if ($mode === false || $mode == 'ignore') { return $this->model; } if ($mode == 'load') { return $this->model->load($this->get()); } if ($mode === null) { if ($this->get()) { $this->model->tryLoad($this->get()); } return $this->model; } if ($mode == 'create') { if ($this->get()) { $this->model->tryLoad($this->get()); } if (!$this->model->loaded()) { $this->model->save(); $this->set($this->model->id); $this->owner->save(); return $this->model; } } if ($mode == 'link') { /** @type Model $m */ $m = $this->add($this->model_name); if ($this->get()) { $m->tryLoad($this->get()); } $t = $this; if (!$m->loaded()) { $m->addHook('afterSave', function ($m) use($t) { $t->set($m->id); $t->owner->saveLater(); }); } return $m; } }
/** * Before save hook * * @param Model $m * * @return void */ function beforeSave($m) { // if new record, then choose volume and generate name if (!$m->loaded()) { // volume $m->set('filestore_volume_id', $m->getAvailableVolumeID()); // generate random original_filename in case you import file contents as string if (!$m['original_filename']) { $m->set('original_filename', mt_rand()); } // generate filename (with relative path) $m->set('filename', $m->generateFilename()); } // perform import itself if ($m->import_mode) { $m->performImport(); } }
/** * Only the currently loaded record will be processed by scheduler. * * $processor->scheduleOne($books, 'sendEmail'); // will delete all books * * @return Queue model (so that you can track it by ID) */ function scheduleOne(\Model $model, $method = 'process') { if (!$model->loaded()) { throw $this->exception('Model must be loaded'); } if ($model instanceof \SQL_Model) { $model->setActualFields(array('id', $model->title_field)); } $class = get_class($model); $caption = $model->caption ?: $class; $q = $this->queue; $id = $model->id; $q->set('model_id', $id); $q->set('model_class', $class); $q->set('model_method', $method); $q->set('name', $method . ' ' . ' (' . $caption . ') #' . $id); $q->set('status', 'scheduled'); // skip draft $q->saveAndUnload(); }