/** * Commits a transaction. * * @throws DbException if the transaction is not active */ public function commit() { if (!$this->getIsActive()) { throw new DbException('Failed to commit transaction: transaction was inactive.'); } $this->_level--; if ($this->_level == 0) { Trace::trace('db', ['msg' => 'Commit transaction', 'method' => __METHOD__]); $this->connection->pdo->commit(); $this->connection->trigger(Connection::EVENT_COMMIT_TRANSACTION); return; } $schema = $this->connection->getSchema(); if ($schema->supportsSavepoint()) { Trace::trace('db', ['msg' => 'Release savepoint ' . $this->_level, 'method' => __METHOD__]); $schema->releaseSavepoint('LEVEL' . $this->_level); } else { if (class_exists('\\rock\\log\\Log')) { $message = BaseException::convertExceptionToString(new DbException('Transaction not committed: nested transaction not supported')); Log::info($message); } } }
/** * Logout user. * @param bool $destroy destroy session. */ public function logout($destroy = true) { if (!$this->enableSession) { return; } $ip = $this->request->getUserIP(); $id = $this->get('id'); Log::info(BaseException::convertExceptionToString(new BaseException("User '{$id}' logged out from {$ip}."))); if ($destroy === true && $this->storage instanceof Session) { $this->storage->destroy(); return; } $this->removeAll(); }
/** * Saves the changes to this active record into the associated database table. * * This method performs the following steps in order: * * 1. call {@see \rock\components\Model::beforeValidate()} when `$runValidation` is true. If validation * fails, it will skip the rest of the steps; * 2. call {@see \rock\components\Model::afterValidate()} when `$runValidation` is true. * 3. call {@see \rock\db\common\BaseActiveRecord::beforeSave()}. If the method returns false, it will skip the * rest of the steps; * 4. save the record into database. If this fails, it will skip the rest of the steps; * 5. call {@see \rock\db\common\BaseActiveRecord::afterSave()}; * * In the above step 1, 2, 3 and 5, events {@see \rock\components\Model::EVENT_BEFORE_VALIDATE}, * {@see \rock\db\common\BaseActiveRecord::EVENT_BEFORE_UPDATE}, {@see \rock\db\common\BaseActiveRecord::EVENT_AFTER_UPDATE} and {@see \rock\components\Model::EVENT_AFTER_VALIDATE} * will be raised by the corresponding methods. * * Only the {@see \rock\db\common\BaseActiveRecord::$dirtyAttributes}(changed attribute values) will be saved into database. * * For example, to update a customer record: * * ```php * $customer = Customer::findOne($id); * $customer->name = $name; * $customer->email = $email; * $customer->update(); * ``` * * Note that it is possible the update does not affect any row in the table. * In this case, this method will return 0. For this reason, you should use the following * code to check if update() is successful or not: * * ```php * if ($this->update() !== false) { * // update successful * } else { * // update failed * } * ``` * * @param boolean $runValidation whether to perform validation before saving the record. * If the validation fails, the record will not be inserted into the database. * @param array $attributeNames list of attributes that need to be saved. Defaults to null, * meaning all attributes that are loaded from DB will be saved. * @return integer|boolean the number of rows affected, or false if validation fails * or {@see \rock\db\common\BaseActiveRecord::beforeSave()} stops the updating process. * @throws DbException if {@see \rock\db\common\BaseActiveRecord::optimisticLock()}(optimistic locking) is enabled and the data * being updated is outdated. * @throws \Exception in case update failed. */ public function update($runValidation = true, array $attributeNames = null) { if ($runValidation && !$this->validate($attributeNames)) { if (class_exists('\\rock\\log\\Log')) { $message = BaseException::convertExceptionToString(new DbException('Model not updated due to validation error.')); Log::info($message); } return false; } if (!$this->isTransactional(self::OP_UPDATE)) { return $this->updateInternal($attributeNames); } $transaction = static::getConnection()->beginTransaction(); try { $result = $this->updateInternal($attributeNames); if ($result === false) { $transaction->rollBack(); } else { $transaction->commit(); } return $result; } catch (\Exception $e) { $transaction->rollBack(); throw $e; } }