Last active
October 23, 2017 20:44
-
-
Save lav45/4fc899520eb3d73065957cb93a9ebc49 to your computer and use it in GitHub Desktop.
Product->getCategoryList() - работа со связанными списками.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace common\models; | |
use yii\db\ActiveRecord; | |
class Category extends ActiveRecord | |
{ | |
/** | |
* @return array | |
*/ | |
public static function getList() | |
{ | |
return static::find() | |
->select(['name', 'id']) | |
->indexBy('id') | |
->column(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace common\models; | |
use yii\db\ActiveRecord; | |
class Product extends ActiveRecord | |
{ | |
public function rules() | |
{ | |
return [ | |
// category_id может быть выбран только из списка getCategoryList() | |
// который предаставляется моделью | |
[['category_id'], 'in', 'range' => array_keys($this->getCategoryList())] | |
]; | |
} | |
/** | |
* @return array | |
*/ | |
public function getCategoryList() | |
{ | |
return Category::getList(); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getCategories() | |
{ | |
return $this->hasOne(Category::className(), ['id' => 'category_id']); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace common\models; | |
use yii\base\Model; | |
class ProductSearch extends Model | |
{ | |
/** | |
* @var int | |
*/ | |
public $category_id; | |
/** | |
* @var Product | |
*/ | |
private $model; | |
/** | |
* @return Product | |
*/ | |
protected function getModel() | |
{ | |
if ($this->model === null) { | |
$this->model = new Product(); | |
} | |
return $this->model; | |
} | |
/** | |
* @return array | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['category_id'], 'integer'], | |
]; | |
} | |
/** | |
* @return array | |
*/ | |
public function getCategoryList() | |
{ | |
return $this->getModel()->getCategoryList() + ['' => 'All']; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @var $this yii\web\View | |
* @var $model common\models\Product | |
*/ | |
use yii\helpers\Html; | |
use yii\widgets\ActiveForm; | |
?> | |
<div> | |
<?php $form = ActiveForm::begin(); ?> | |
<?= $form->field($model, 'category_id')->dropDownList($model->getCategoryList()) ?> | |
<div class="form-group"> | |
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->getIsNewRecord() ? 'btn btn-success' : 'btn btn-primary']) ?> | |
</div> | |
<?php ActiveForm::end(); ?> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @var $this yii\web\View | |
* @var $searchModel common\models\ProductSearch | |
* @var $dataProvider yii\data\ActiveDataProvider | |
*/ | |
use yii\grid\GridView; | |
?> | |
<?= GridView::widget([ | |
'dataProvider' => $dataProvider, | |
'filterModel' => $searchModel, | |
'columns' => [ | |
// ... | |
[ | |
'attribute' => 'category_id', | |
'filter' => $searchModel->getCategoryList(), // получаем необходимые данные из ProductsSearch !!! | |
], | |
// ... | |
], | |
]); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment