Gridview dataprovider и т.п. не использую, все пишу руками.
$user->password_hash = Yii::$app->security->generatePasswordHash($password, $hash);
public $old_password;
private $user;
public function __construct(User $user, $config = []){
$this->user = $user;
parent::__construct($config);
}
public function rules() {
return [
// ...
['old_password', 'validateOldPassword'],
];
}
public function validateOldPassword($attribute, $params, Validator $validator){
if (!\Yii::$app->security->validatePassword($this->{$attribute}, $this->user->password_hash)){
$this->addError($attribute, 'Старый пароль указан неверно!');
}
}
$config = [
'components' => [],
'modules' => [],
'on afterRequest' => function (Event $event) {
Yii::$app->response->cookie->add(new Cookie([
'name' => 'cookie_name',
'value' => 'cookie_value'
]));
}
];
'user' => [
'identityClass' => 'common\models\User',
'loginUrl' => '/site/login',
'identityCookie' => [
'name' => '_identity-frontend',
],
],
'modules' => [
'api1' => [
'class' => 'api\modules\Api1',
'components' => [
'user' => [
'identityClass' => 'common\models\Api1User',
'loginUrl' => '/site/login',
'identityCookie' => [
'name' => '_identity-api1',
],
],
]
],
'api2' => [
'class' => 'api\modules\Api2',
'components' => [
'user' => [
'identityClass' => 'common\models\Api2User',
'loginUrl' => '/site/login',
'identityCookie' => [
'name' => '_identity-api2',
],
],
]
],
]
'filter' => DatePicker::widget([
'model' => $searchModel,
'attribute' => 'created_start',
'attribute2' => 'created_end',
'separator' => '-',
'type' => DatePicker::TYPE_RANGE,
'pluginOptions' => ['format' => 'yyyy-mm-dd'],
'options' => ['autocomplete' => 'off'],
'options2' => ['autocomplete' => 'off'],
]),
is_old = true
is_old = false
, а при существовании записи в базе - обновляем is_old = false
, затем далее удаляем все записи с is_old = true
'components' => [
'request' => [
'csrfParam' => '_csrf-api',
'parsers' => [
'application/json' => [
'class' => \yii\web\JsonParser::class,
'asArray' => true,
],
],
],
// ...
{
"name": "Nick"
}
$name = Yii::$app->request->post('name')
будет работать. interface SoftDeletableInterface
{
public function softDelete() : bool;
public function getChildren() : array;
}
abstract class SoftDeletableModel extends ActiveRecord implements SoftDeletableInterface
{
public function getChildren() : array
{
return [];
}
final public function softDelete(bool $inTransaction = false) : bool
{
if ($inTransaction) {
$this->softDeleteInternal();
return true;
}
$transaction = \Yii::$app->db->beginTransaction();
try {
$this->softDeleteInternal();
$transaction->commit();
return true;
} catch (\Exception $exception) {
$transaction->rollBack();
return false;
}
}
private function softDeleteInternal() : void
{
$this->updateAttributes(['is_deleted' => true]);
foreach ($this->getChildren() as $children) {
foreach ((array)$children as $child) {
$child->softDelete(true);
}
}
}
}
class Category extends SoftDeletableModel
{
public function getChildren() : array
{
return $this->groups;
}
public function getGroups()
{
return $this->hasMany(Group::class, ['category_id' => 'id']);
}
}
class Group extends SoftDeletableModel
{
public function getChildren() : array
{
return [$this->services, $this->users] /* array of array */;
}
public function getServices()
{
return $this->hasMany(Service::class, ['group_id' => 'id']);
}
public function getUsers()
{
return $this->hasMany(User::class, ['group_id' => 'id']);
}
}
class Service extends SoftDeletableModel
{
}
class User extends SoftDeletableModel
{
}
enablePrettyUrl
и взглянуть на метод UrlManager::createUrl()
, то мы увидим процесс формирования ссылки:$url = "$baseUrl?{$this->routeParam}=" . urlencode($route);
if (!empty($params) && ($query = http_build_query($params)) !== '') {
$url .= '&' . $query;
}
return $url . $anchor;
'assetManager' => [
'bundles' => [
'yii\web\JqueryAsset' => [
'sourcePath' => '@frontend/assets',
'js' => [
'js/my-jquery.js',
],
],
],
],
// находим вакансию
$vacancy = $model->getVacancyById($id);
// здесь вы должны удостовериться, что вакансия по заданному ID найдена
// а вы сразу пытаетесь получить атрибут user_id, при том что $vacancy в тот момент может быть NULL
// на всякий случай, проверяем, является ли
// пользователь автором вакансии
if($currentUser->getId() != $vacancy->user_id ) {
return $this->goHome();
}
public function getVacancyById($id)
{
$vacancy = $this->find()->where(['id' => $id])->one();
if (!$vacancy){
throw new NotFoundHttpException('Вакансия не найдена');
}
return $vacancy;
}