SELECT DISTINCT code_medication, MIN(price) FROM tailings t GROUP BY code_medication LIMIT 50;
Product::find()
->distinct()
->select('code, MIN(price)')
->from('product')
->groupBy('code');
SELECT * FROM tailings t WHERE t.price = (SELECT MIN(t2.price) FROM tailings t2 WHERE t2.code_medication = t.code_medication) GROUP BY t.code_medication;
$subQuery = (new Query())->select('MIN(t2.price)')->from('tailings t2')->where('t2.code_medication = t.code_medication');
$query = Product::find()->from('tailings t')->where(['t.price' => $subQuery])->groupBy('t.code_medication');
$countQuery = clone $query;
$pages = new Pagination([
'totalCount' => $countQuery->count(),
'pageSize' => 15,
'forcePageParam' => false,
'pageSizeParam' => false,
]);
$products = $query->offset($pages->offset)->limit($pages->limit)->all();
['birth_date', 'date', 'timestampAttribute' => 'birth_date', 'timestampAttributeFormat' => 'php:Y-m-d'],
['death_date', 'date', 'timestampAttribute' => 'death_date', 'timestampAttributeFormat' => 'php:Y-m-d'],
['death_date', function($attribute, $params) {
if (\DateTime::createFromFormat("Y-m-d", $this->{$attribute})->getTimestamp() < \DateTime::createFromFormat("Y-m-d", $this->birth_date)->getTimestamp()) {
$this->addError($attribute, 'Дата смерти не может быть меньше даты рождения.');
}
}]
if (\DateTime::createFromFormat("Y-m-d", $this->death_date)->getTimestamp() < \DateTime::createFromFormat("Y-m-d", $this->birth_date)->getTimestamp()) {
$this->addError($attribute, 'Дата смерти не может быть меньше даты рождения.');
}
$aq = Users::find()->distinct()->alias('u');
if ($this->role !== null) {
$aq->leftJoin('auth_assignment a', 'a.user_id = u.id AND a.item_name = :role', [':role' => $this->role]);
$aq->where("a.user_id IS NOT NULL");
}
$message = $status = null;
if (Yii::$app->request->post()) {
if ($model->save()) {
$status = 'success';
$message = 'Спасибо операция окончена';
} else {
$status = 'error';
$message = 'Извините произошла ошибка';
}
}
$this->render('html', [
'model' => $model,
'status' => $status,
'message' => $message,
]);
<?php if ($status !== null && $message !== null): ?>
<div class="alert alert-<?=$status?>">
<strong><?=$status?>!</strong> <?=$message?>
</div>
<?php endif; ?>
.......
.......
public function deleteImage($event)
{
$imagePath = Yii::getAlias($this->_uploadPath . '/' . $this->_oldImage);
if (is_file($imagePath)) {
unlink($imagePath);
}
}
$model = new User(['ip' => Yii::$app->request->userIP]);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
......
}
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->ip = Yii::$app->request->userIP;
$model->save(false);
}
//Set component
public function init()
{
Yii::$app->setComponents(
[
'Notification' => [
'class'=>'app\components\Notification',
],
]);
}
[
'class' => TimestampBehavior::className(),
'value' => date('Y.m.d H:i'),
],
[
'class' => TimestampBehavior::className(),
'value' => function($event) {
return date('Y.m.d H:i');
},
],
В таком варианте очень неудобно использовать permission для разграничения прав, так как permission можно привязать только к группе пользователя, но не к самому пользователю.
$authManager = Yii::$app->authManager;
$userID = 1;
$permission = $authManager->getPermission('language_ru');
$authManager->assign($permission, $userID);
$data = $model->getRatings()
->select('count(rating.*), SUM(rating.quantity)')
->alias('rating')
->createCommand()
->queryOne();
$ratings = $model->ratings;
$count = $sum = 0;
if (is_array($ratings)) {
$count = count($ratings);
foreach($ratings as $rating) {
$sum += (int) $rating->quantity;
}
}
echo $count, $sum;
$model = new Model;
if ($model->load(Yii::$app->request->post)) {
$validators = $model->getValidators();
foreach($validators as $validator) {
// Ищем тут нужный нам валидатор даты и задаем ему тайм зону
$validator->timeZone = $model->timezone;
}
$model->save();
}