本文整理汇总了PHP中yii\db\ActiveRecord::getDb方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord::getDb方法的具体用法?PHP ActiveRecord::getDb怎么用?PHP ActiveRecord::getDb使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\ActiveRecord
的用法示例。
在下文中一共展示了ActiveRecord::getDb方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: beginTransaction
protected function beginTransaction()
{
$db = $this->owner->getDb();
if ($db->getTransaction() === null) {
$this->transaction = $db->beginTransaction();
}
}
示例2: getDb
/**
* @inheritdoc
*/
public static function getDb()
{
if (Configs::db() !== null) {
return Configs::db();
} else {
return parent::getDb();
}
}
示例3: getDb
/**
* @inheritdoc
*/
public static function getDb()
{
if (Configs::instance()->db !== null) {
return Configs::instance()->db;
} else {
return parent::getDb();
}
}
示例4: save
public function save($runValidation = true, $attributes = null)
{
if ($runValidation && !$this->validate($attributes)) {
Yii::info('Dynamic model data were not save due to validation error.', __METHOD__);
return false;
}
$db = $this->entityModel->getDb();
$transaction = $db->beginTransaction();
try {
foreach ($this->handlers as $handler) {
$handler->valueHandler->save();
}
$transaction->commit();
} catch (\Exception $e) {
$transaction->rollBack();
throw $e;
}
}
示例5: getDb
/**
* @return \yii\db\Connection the database connection used by this AR class.
*/
public static function getDb()
{
if (isset(Yii::$app->params['audittrail.db'])) {
return Yii::$app->get(Yii::$app->params['audittrail.db']);
} else {
return parent::getDb();
}
// return Yii::$app->get('dbUser');
}
示例6: add
public static function add($tags, $blog_id)
{
$data = [];
if (Tags::validateTags($tags)) {
$tags = explode(',', $tags);
}
foreach ($tags as $tag) {
array_push($data, ['name' => $tag, 'blog_id' => $blog_id]);
}
parent::getDb()->createCommand()->batchInsert(self::tableName(), ['name', 'blog_id'], $data)->execute();
}
示例7: 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;
}
示例8: deleteAll
/**
* Delete all SEO data for ActiveRecord
* @param ActiveRecord $owner
*/
public static function deleteAll(ActiveRecord $owner)
{
$owner->getDb()->createCommand()->delete(self::tableName($owner), ['model_id' => $owner->getPrimaryKey()])->execute();
}
示例9: aggregate
/**
* Convert aggregate query from Kendo UI to DB aggregate functions
*
* @param array $aggregates usually values of $_POST['aggregates']
* @param ActiveRecord $model model for generation aggregate functions
* @return array aggregate functions for Query::select()
*/
public static function aggregate($aggregates, ActiveRecord $model)
{
if (!$aggregates || !is_array($aggregates)) {
return [];
}
$db = $model->getDb();
$attributes = $model->attributes();
$aggregateFunctions = static::AGGREGATE_FUNCTIONS;
$functions = [];
foreach ($aggregates as $aggregate) {
if (!empty($aggregate['aggregate']) && isset($aggregateFunctions[$aggregate['aggregate']]) && !empty($aggregate['field']) && in_array($aggregate['field'], $attributes)) {
$funcName = $aggregateFunctions[$aggregate['aggregate']];
$functions[] = $funcName . '(' . $db->quoteColumnName($aggregate['field']) . ') ' . ' AS ' . $db->quoteColumnName($aggregate['aggregate'] . '_' . $aggregate['field']);
}
}
return $functions;
}
示例10: unlink
/**
* Removed
*
* @param mixed $id the id of the class to remove from link table
*/
protected function unlink($id)
{
ActiveRecord::getDb()->createCommand()->delete($this->ownerLinkTable, [$this->ownerLinkTableAttribute => $id])->execute();
}
示例11: getTranslation
/**
* Relation to model translation
*
* @param $locale
* @return ActiveQuery
*/
public function getTranslation($locale = null)
{
$locale = $locale ?: Yii::$app->language;
return $this->owner->hasOne($this->translationModelName, [$this->translationOwnerField => $this->ownerPrimaryKey])->from(['content' => call_user_func([$this->translationModelName, 'tableName'])])->innerJoin(['lang' => Language::tableName()])->where('lang.locale = :locale', [':locale' => $locale])->andWhere('content.' . $this->owner->getDb()->quoteColumnName($this->languageField) . ' = lang.id');
}
示例12: link
/**
* Links uploaded file to its owner.
*
* @param mixed $ownerId
* @param mixed $id
*/
protected function link($ownerId, $id)
{
ActiveRecord::getDb()->createCommand()->insert($this->ownerLinkTable, [$this->ownerLinkTableAttribute => $ownerId, $this->ownerLinkTableAttribute => $id])->execute();
}
示例13: saveSettingsModel
/**
* Save model of settings
* @param ActiveRecord $model Model to save
* @param array $requestParams Request parameters
* @return bool
*/
protected function saveSettingsModel(ActiveRecord $model, array $requestParams)
{
$success = false;
$transaction = $model->getDb()->beginTransaction();
try {
$model->load($requestParams);
if ($model->save()) {
$transaction->commit();
Yii::$app->interfaceSettings->refreshUserInterfaceSettings($model->user_id, $model->interface_id);
$success = true;
} else {
throw new Exception();
}
} catch (Exception $e) {
if ($transaction->isActive) {
$transaction->rollBack();
}
}
// trigger an event
$this->trigger(self::EVENT_SAVE_SETTING, new ExtraDataEvent(['extraData' => ['model' => $model, 'success' => $success]]));
return $success;
}
示例14: addDirtyModel
/**
* @param \yii\db\ActiveRecord $model
*/
public function addDirtyModel($model)
{
// --- debug - start ------------------------------------------------------
// make sure there's always only one instance referencing a DB record
$dsn = $model->getDb()->dsn;
$table = $model->tableName();
$pk = $model->getPrimaryKey();
$modelId = "{$dsn}:{$table}:{$pk}";
if (array_key_exists($modelId, $this->_dirtyModelsInfo)) {
$storedModel = $this->_dirtyModelsInfo[$modelId];
if ($storedModel !== $model) {
throw new \yii\base\NotSupportedException("Can't handle two instances for tabel '{$table}', id '{$pk}' (dsn '{$dsn}').");
}
}
$this->_dirtyModelsInfo[$modelId] = $model;
// --- debug - end --------------------------------------------------------
$this->_dirtyModels->attach($model);
}
示例15: init
/**
* @overriding
* @see \yii\base\Object::init()
*/
public function init()
{
parent::init();
try {
//Initialize template model of the mail AR.
$this->_templateModel = Yii::createObject($this->modelClass);
$this->_db = $this->_templateModel->getDb();
if ($this->sentByBehavior) {
$this->sentByBehavior = Yii::createObject($this->sentByBehavior);
}
} catch (\ReflectionException $refE) {
$route = explode('/', Yii::$app->getRequest()->resolve()[0]);
if (isset(Yii::$app->coreCommands()[$route[0]]) || empty($route[0])) {
return;
}
$action = isset($route[1]) ? $route[1] : '';
if (!$this->installerMode) {
$err = "Model class <{$this->modelClass}> not found." . PHP_EOL;
$err .= "Please install the module first before using it.";
$err .= "(Instruction in QuickStart section of README file.)" . PHP_EOL;
$err .= "If the module is already installed, ";
$err .= "please check if the attribute \"modelClass\" defines an available Model class." . PHP_EOL;
$this->stderr($err, Console::FG_RED);
Yii::$app->end(1);
} elseif (!in_array($action, array_keys($this->installerActions))) {
$err = 'Only actions: ' . implode(', ', array_keys($this->installerActions));
$err .= ' are available in current installer mode.' . PHP_EOL;
$err .= 'Please set option "installerMode" to false (default) to use other commands.' . PHP_EOL;
$this->stderr($err, Console::FG_RED);
Yii::$app->end(1);
}
} catch (Exception $e) {
throw $e;
}
}