本文整理汇总了PHP中yii\db\ActiveRecord::save方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord::save方法的具体用法?PHP ActiveRecord::save怎么用?PHP ActiveRecord::save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\ActiveRecord
的用法示例。
在下文中一共展示了ActiveRecord::save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: save
/**
* Saves the current model
* @param boolean $runValidation whether to perform validation before saving the record.
* If the validation fails, the record will not be saved to database.
* @param array $attributeNames list of attribute names that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @param boolean $withSubtree whether subtree sticks to model (e.g. if model parent is changes - should subtree follow model or stay with model's parent)
* @return boolean whether the saving succeeds
*/
public function save($runValidation = true, $attributeNames = null, $withSubtree = true)
{
$transaction = $this->getDb()->beginTransaction();
try {
$result = false;
if ($this->isNewRecord) {
$this->setNodeLevel($this->calculateLevel());
if (parent::save($runValidation, $attributeNames)) {
$this->setAncestorsLinks();
$result = true;
}
} else {
if ($this->{$this->getIdParentAttribute()} != $this->getOldParentId() && $this->prepareParentChange($withSubtree) && parent::save($runValidation, $attributeNames)) {
$result = true;
}
}
if ($result === true) {
$transaction->commit();
return true;
} else {
$transaction->rollBack();
return false;
}
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
}
示例2: performModelSave
/**
* @param ActiveRecord $model
* @param array $params
* @return bool|mixed
*/
public function performModelSave(ActiveRecord $model, array $params)
{
$ajax = Yii::$app->request->isAjax;
if ($model->save()) {
if ($ajax) {
$data = ['success' => true, 'message' => Yii::t('management', 'Model saved')];
if (isset($params['success']) && is_callable($params['success'])) {
$data = array_merge($data, call_user_func($params['success'], $model));
}
return $this->sendJson($data);
}
if (isset($params['fallback']) && is_callable($params['fallback'])) {
return call_user_func($params['fallback'], $model);
}
} else {
if ($ajax) {
$data = ['success' => false, 'message' => Yii::t('management', 'Model not saved'), 'messages' => ActiveForm::validate($model)];
if (isset($params['error']) && is_callable($params['error'])) {
$data = array_merge($data, call_user_func($params['error'], $model));
}
return $this->sendJson($data);
}
}
return false;
}
示例3: save
/**
* @param boolean $runValidation
* @throws RowException
*/
public function save($runValidation = true)
{
if ($runValidation) {
$this->validate();
}
$this->_instance->save(false);
}
示例4: save
public function save($runValidation = true, $attributeNames = NULL)
{
$this->user_id = Yii::$app->getUser()->id;
$this->created_at = time();
//$this->content = strip_tags($this->content); //去掉html标签
parent::save($runValidation, $attributeNames);
}
示例5: save
public function save($runValidation = true, $attributeNames = null)
{
if ($this->tmpname != null) {
rename("{$this->tempPath}/{$this->tmpname}", "{$this->basePath}/{$this->tmpname}");
$this->path = $this->tmpname;
}
return parent::save($runValidation, $attributeNames);
}
示例6: createRecord
/**
* @param \yii\base\ModelEvent $event
* @throws Exception
*/
public function createRecord($event)
{
foreach ($this->attributes as $key => $value) {
if (is_string($value)) {
$this->target->{$key} = ArrayHelper::getValue($event->sender, $value);
} else {
if (CheckHelper::isCallable($value)) {
$this->target->{$key} = call_user_func($value, $event->sender);
} else {
throw new Exception();
}
}
}
if (!$this->target->save()) {
throw new Exception('Save target error: ' . Json::encode($this->target));
}
}
示例7: save
public function save($runValidation = true, $attributeNames = null)
{
$result = parent::save($runValidation, $attributeNames);
if ($result) {
// При изменении препаратов, менять пользователя и дату изменения в карте глаукомного пациента
Glaukuchet::findOne($this->id_glaukuchet)->UpdateChangeAttributes();
}
return $result;
}
示例8: save
/**
* @param bool $runValidation
* @param null $attributeNames
*
* @return bool|void
*/
public function save($runValidation = true, $attributeNames = null)
{
if ($this->getIsNewRecord()) {
$this->date_create = date('Y-m-d H:i:s');
}
if (is_null($this->date_execute)) {
$this->date_execute = date('Y-m-d H:i:s');
}
return parent::save($runValidation, $attributeNames);
}
示例9: save
public function save($runValidation = true, $attributeNames = null)
{
if (!$this->history) {
$this->history = [];
}
$oldUrl = $this->getOldAttribute('url');
if ($this->url !== $oldUrl) {
$this->history += [$oldUrl];
}
return parent::save($runValidation, $attributeNames);
}
示例10: save
public function save($runValidation = true, $attributeNames = null)
{
if (parent::save($runValidation, $attributeNames)) {
//dopo aver salvato le modifiche, processa gli upload delle foto
foreach ($this->addimage as $tmpfoto) {
$foto = new Foto();
$foto->cartellone = $this->id;
$foto->tmpname = $tmpfoto;
$foto->save();
}
return true;
}
return false;
}
示例11: processModel
/**
* @param ActiveRecord $model
*/
private function processModel(ActiveRecord $model)
{
$model->detachBehaviors();
$count = $model->{$this->countRelation}();
if ($count instanceof ActiveQuery) {
$count = $count->count();
} else {
if (is_array($count)) {
$count = json_encode($count);
}
}
$model->{$this->columnToUpdate} = $count;
$model->save(false, [$this->columnToUpdate]);
}
示例12: save
public function save($runValidation = true, $attributeNames = NULL)
{
self::formatModel();
$new_assigned = '';
$send_notification = 0;
if ($this->assigned == '1') {
$new_assigned = \app\memoboard\functions::getArray($this->assigned_to);
$send_notification = $this->send_notification;
}
$connection = \Yii::$app->db;
$transaction = $connection->beginTransaction();
if (parent::save() && self::saveAssign($new_assigned, $send_notification)) {
$transaction->commit();
return true;
}
$transaction->rollback();
return false;
}
示例13: save
public function save($runValidation = true, $attributeNames = null)
{
$uploadedFile = UploadedFile::getInstance($this, 'preview');
if ($uploadedFile) {
$filePath = 'uploads/' . $uploadedFile->baseName . '.' . $uploadedFile->extension;
if (file_exists($filePath)) {
$filePath = 'uploads/' . $uploadedFile->baseName . 'N.' . $uploadedFile->extension;
}
$uploadedFile->saveAs($filePath);
$this->preview = '/' . $filePath;
} else {
$oldPreview = $this->getOldAttribute('preview');
if ($oldPreview) {
$this->preview = $oldPreview;
}
}
return parent::save($runValidation, $attributeNames);
}
示例14: insertTo
/**
* @param $position
* @return bool
* @throws \yii\db\Exception
*/
public function insertTo($position)
{
if ($this->getValue() == $position) {
return true;
}
// 59 -> 56 = 58 57 56
// 56 -> 59 = 57 58 59
$start = $this->getValue();
$end = $position;
if ($start == null || $start == 0) {
$this->setInsertSort();
$this->owner->save();
return true;
}
$direction = $start > $end ? static::DirectionDown : static::DirectionUp;
$query = $this->getQuery();
$query->andWhere(['between', $this->attributeName, min($start, $end), max($start, $end)]);
$query->getSortBehavior()->sort();
/**
* @var ISortableActiveRecord[]|ActiveRecord[] $items
*/
$items = $query->all();
$transaction = $this->owner->getDb()->beginTransaction();
try {
foreach ($items as $index => $item) {
if ($item->getPrimaryKey() == $this->owner->getPrimaryKey()) {
$item->getSortBehavior()->setSort($position);
$item->save();
} else {
$item->getSortBehavior()->setSort($item->getSortBehavior()->getValue() + ($direction == static::DirectionDown ? 1 : -1));
$item->save();
}
}
/* @TODO сделать фикс позиций, что бы не перескакивало и не было дублей */
$transaction->commit();
} catch (\Exception $ex) {
$transaction->rollBack();
return false;
}
return true;
}
示例15: storeDataRecord
/**
* Store data record and track change statistics
*
* @param ActiveRecord $ActiveRecord
*
* @return bool false if not saved
*/
protected function storeDataRecord(ActiveRecord $ActiveRecord)
{
if ($ActiveRecord->getDirtyAttributes()) {
$unsaved_record = clone $ActiveRecord;
// Save record
if (!$ActiveRecord->save()) {
// Create error message
$message = "Save error: " . json_encode($ActiveRecord->errors) . "\n";
$message .= "Record data: " . json_encode($ActiveRecord->getAttributes()) . "\n";
trigger_error($message, E_USER_WARNING);
$this->incStat('error_' . $ActiveRecord->tableName());
return false;
}
// Store statistics
if ($unsaved_record->isNewRecord) {
$this->incStat('new_' . $ActiveRecord->tableName());
} else {
$this->incStat('update_' . $ActiveRecord->tableName());
}
}
return true;
}