<?php
namespace common\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Course;
/**
* CourseSearch represents the model behind the search form about `common\models\Course`.
*/
class CourseSearch extends Course
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'category_id', 'status_id', 'author', 'price'], 'integer'],
[['name', 'title', 'url', 'content', 'keywords', 'description', 'link', 'partlink'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Course::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'category_id' => $this->category_id,
'status_id' => $this->status_id,
'author' => $this->author,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'url', $this->url])
->andFilterWhere(['like', 'content', $this->content])
->andFilterWhere(['like', 'keywords', $this->keywords])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'link', $this->link])
->andFilterWhere(['like', 'partlink', $this->partlink]);
return $dataProvider;
}
}
public function actionIndex()
{
$searchModel = new CourseSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->setSort([
'defaultOrder' => [
'id' => SORT_DESC
]
]);
$dataProvider->setPagination([
'pageSize' => 10,
'forcePageParam' => false,
'pageSizeParam' => false
]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
<?= $form->field($model, 'author',['options' => ['class' => 'col-xs-2']])->dropDownList(\common\models\Author::getList());?>
public static function getList()
{
return ArrayHelper::map(self::find()->all(), 'id', 'name');
}
[
'attribute' => 'author',
'value' => 'authorName',
'filter' => \common\models\Author::getList(),
'options' => ['width' => '200']
],
public function getAuthor()
{
return $this->hasOne(Author::className(), ['id' => 'author']);
}
public function getAuthorName()
{
return (isset($this->author)) ? $this->author->name : 'не задано';
}
$query = Author::find()->with('course');
public function actionNewadd()
{
$id = Yii::$app->request->get('id');
$offer = Yii::$app->request->get('offer');
$qty = Yii::$app->request->get('qty');
$qty = !$qty ? 1 : $qty;
$tovar = Tovar::find()->where(['id'=>$id])->asArray()->one();
$offer = OfferPrise::find()->where(['id'=>$offer])->asArray()->one();
$offer_size = OfferSize::find()->where(['id_offer_prise'=>$offer['id']])->asArray()->one();
if(empty($tovar)) return false;
$session = Yii::$app->session;
$session->open();
$cart = new Cart();
$cart->addCart($tovar, $qty, $offer, $offer_size);
if(!Yii::$app->request->isAjax) {
return $this->redirect(Yii::$app->request->referrer);
}
$this->layout = false;
return $this->render('cart-modal', compact('session'));
}
public function addCart($tovar, $qty = 1, $offer, $offer_size)
{
if (isset($_SESSION['cart'][$offer['id']])) {
$_SESSION['cart'][$offer['id']]['qty'] += $qty;
} else {
$_SESSION['cart'][$offer['id']] = [
'qty' => $qty,
'name' => $tovar['name'],
'tovar' => $tovar['id'],
'size' => $offer_size['value'],
'prise' => $offer['value'],
];
}
$_SESSION['cart.qty'] = isset($_SESSION['cart.qty']) ? $_SESSION['cart.qty'] + $qty : $qty;
$_SESSION['cart.sum'] = isset($_SESSION['cart.sum']) ? $_SESSION['cart.sum'] + $qty * $offer['value'] : $qty * $offer['value'];
}
$dataProvider = new ActiveDataProvider([
'query' => Orders::find(),
'pagination' => [
'pageSize' => 10,
'forcePageParam' => false,
'pageSizeParam' => false
],
'sort' => [
'defaultOrder' => [
'statys' => SORT_ASC
]
]
]);