本文整理汇总了PHP中yii\db\ActiveRecord类的典型用法代码示例。如果您正苦于以下问题:PHP ActiveRecord类的具体用法?PHP ActiveRecord怎么用?PHP ActiveRecord使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ActiveRecord类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: processOutput
/**
* Process output. Throw error when not success with unknown reason.
* @param boolean $success
* @param ActiveRecord $model
*
* @return ActiveRecord
* @throws \yii\web\ServerErrorHttpException
*/
protected static function processOutput($success, $model)
{
if (!$success && !$model->hasErrors()) {
throw new ServerErrorHttpException('Error with unknown reason.');
}
return $model;
}
示例2: getCreateUpdateResponse
/**
* @param ActiveRecord $model
* @param array $actions Custom actions array in the form of
* ```php
* 'name' => function() {
*
* }
* ```
* @param bool $addDefaultActions If true default actions will be added
* @return \yii\web\Response
* @throws BadRequestHttpException
*/
protected function getCreateUpdateResponse($model, $actions = [], $addDefaultActions = true)
{
$defaultActions = [AdminHtml::ACTION_SAVE_AND_STAY => function () use($model) {
/** @var Controller | CrudControllerTrait $this */
return $this->redirect(['update', 'id' => $model->getPrimaryKey()]);
}, AdminHtml::ACTION_SAVE_AND_CREATE => function () use($model) {
/** @var Controller | CrudControllerTrait $this */
if ($url = Url::previous($this->createUrlParam)) {
Url::remember(null, $this->createUrlParam);
return $this->redirect($url);
}
return $this->redirect(['create']);
}, AdminHtml::ACTION_SAVE_AND_LEAVE => function () use($model) {
/** @var Controller | CrudControllerTrait $this */
if ($url = \Yii::$app->request->get('return')) {
return $this->redirect($url);
}
if ($url = Url::previous($this->indexUrlParam)) {
Url::remember(null, $this->indexUrlParam);
return $this->redirect($url);
}
return $this->redirect(['index']);
}];
if ($addDefaultActions) {
$actions = array_merge($defaultActions, $actions);
}
$actionName = \Yii::$app->request->post(AdminHtml::ACTION_BUTTON_NAME, AdminHtml::ACTION_SAVE_AND_LEAVE);
if (isset($actions[$actionName])) {
return call_user_func($actions[$actionName]);
} else {
throw new BadRequestHttpException('Unknown action: ' . $actionName);
}
}
示例3: init
public function init()
{
parent::init();
$this->owner->on(ActiveRecord::EVENT_AFTER_INSERT, [$this, 'save']);
$this->owner->on(ActiveRecord::EVENT_AFTER_UPDATE, [$this, 'save']);
$this->owner->on(ActiveRecord::EVENT_BEFORE_DELETE, [$this, 'unlink']);
}
示例4: run
/**
* @param integer $type
* @param \yii\db\ActiveRecord $object
*/
public function run($type, $object)
{
$pkey = $object->primaryKey();
$pkey = $pkey[0];
$data = ['table' => $object->tableName(true), 'model_id' => $object->getPrimaryKey(), 'type' => $type, 'date' => date('Y-m-d H:i:s', time())];
switch ($type) {
case self::EVT_INSERT:
$data['field_name'] = $pkey;
$this->saveField($data);
break;
case self::EVT_UPDATE:
foreach ($this->updatedFields as $updatedFieldKey => $updatedFieldValue) {
$data['field_name'] = $updatedFieldKey;
$data['old_value'] = $updatedFieldValue;
$data['new_value'] = $object->{$updatedFieldKey};
$this->saveField($data);
}
break;
case self::EVT_DELETE:
$data['field_name'] = $pkey;
$this->saveField($data);
break;
case self::EVT_UPDATE_PK:
$data['field_name'] = $pkey;
$data['old_value'] = $object->getOldPrimaryKey();
$data['new_value'] = $object->{$pkey};
$this->saveField($data);
break;
}
}
示例5: _delete
protected function _delete(ActiveRecord $model)
{
$id = Yii::$app->request->post('id');
$model->findOne($id)->delete();
// $model::deleteAll($id);
return $this->_success();
}
示例6: relation
public static function relation(ActiveRecord $model, $relation_name, $options = [])
{
/* @var ActiveRecord|YcmModelUtilTrait $model */
$relation = $model->getRelation($relation_name);
$config = [$relation_name, 'widget', 'widgetClass' => Select2::className(), 'data' => RelationHelper::getSelectChoices($model, $relation_name), 'hideSearch' => false, 'options' => ['multiple' => $relation->multiple, 'placeholder' => 'Select...'], 'pluginOptions' => ['allowClear' => true]];
return ArrayHelper::merge($config, $options);
}
示例7: testRenderDualNotController
/**
* @test
*/
public function testRenderDualNotController()
{
$controller = new \yii\db\ActiveRecord();
$controller->attachBehavior('RenderDual', new \yii2renderdual\RenderDual());
$this->setExpectedException('Exception');
$render = $controller->renderDual('view', ['foo' => 'bar'], 'foo bar');
}
示例8: addErrorMessagesFromModel
/**
* @param ActiveRecord $model
*/
public function addErrorMessagesFromModel(ActiveRecord $model)
{
foreach ($model->getErrors() as $fieldWithErrors) {
foreach ($fieldWithErrors as $error) {
$this->addMessage(null, $error, Message::ALERT);
}
}
}
示例9: getUrl
/**
* @param $direction string
* @param $model ActiveRecord
* @return array
*/
protected function getUrl($direction, ActiveRecord $model)
{
$url = !empty($this->url) ? $this->url : ['order'];
$url['direction'] = $direction;
$url['attribute'] = $this->attribute;
$url['id'] = $model->getPrimaryKey();
return $url;
}
示例10: attach
/**
* @inheritdoc
*
* @param ActiveRecord $owner
*
* @throws ErrorException
*/
public function attach($owner)
{
if (!self::$_eventSwitched) {
Event::on($owner->className(), VekActiveRecord::EVENT_TO_SAVE_MULTIPLE, [self::className(), 'logToSaveMultiple']);
Event::on($owner->className(), VekActiveRecord::EVENT_SAVED_MULTIPLE, [self::className(), 'logSavedMultiple']);
}
parent::attach($owner);
}
示例11: getFilenameFor
/**
* @param ActiveRecord $activeRecord
* @param $attribute
* @param null $extension
*
* @return string
*/
public function getFilenameFor($activeRecord, $attribute, $extension = null)
{
$path = Inflector::camel2id((new \ReflectionClass($activeRecord))->getShortName());
$basename = implode('-', $activeRecord->getPrimaryKey(true)) . '-' . $attribute;
if ($extension) {
$basename .= '.' . $extension;
}
return $path . DIRECTORY_SEPARATOR . $basename;
}
示例12: castModel
/**
* Get an array representing the properties of a model.
*
* @param \yii\db\ActiveRecord $model
* @return array
*/
public static function castModel(ActiveRecord $model)
{
$attributes = array_merge($model->getAttributes(), $model->getRelatedRecords());
$results = [];
foreach ($attributes as $key => $value) {
$results[Caster::PREFIX_VIRTUAL . $key] = $value;
}
return $results;
}
示例13: findOrderNum
/**
* Find and set order numeric
*/
public function findOrderNum()
{
if (!$this->owner->{$this->attribute}) {
$maxOrderNum = (int) (new Query())->select("MAX({$this->attribute})")->from($this->owner->tableName())->scalar();
$this->owner->{$this->attribute} = $maxOrderNum + $this->step;
}
}
示例14: init
public function init()
{
$this->id = $this->model->formName();
$this->action = RequestModule::getPopupUrl(['type' => $this->model->tableName()]);
$this->fieldConfig = ['template' => '<div class="row">{input}{error}</div>', 'errorOptions' => ['class' => 'errorMessage']];
parent::init();
}
示例15: run
public function run($action)
{
$this->type = Yii::$app->request->get('type');
$this->behaviorName = Yii::$app->request->get('behaviorName');
$this->galleryId = Yii::$app->request->get('galleryId');
$pkNames = call_user_func([$this->types[$this->type], 'primaryKey']);
$pkValues = explode($this->pkGlue, $this->galleryId);
$pk = array_combine($pkNames, $pkValues);
$this->owner = call_user_func([$this->types[$this->type], 'findOne'], $pk);
$this->behavior = $this->owner->getBehavior($this->behaviorName);
switch ($action) {
case 'delete':
return $this->actionDelete(Yii::$app->request->post('id'));
break;
case 'ajaxUpload':
return $this->actionAjaxUpload();
break;
case 'changeData':
return $this->actionChangeData(Yii::$app->request->post('photo'));
break;
case 'order':
return $this->actionOrder(Yii::$app->request->post('order'));
break;
default:
throw new HttpException(400, 'Action do not exists');
break;
}
}