I’m linking related models together via a link. ONE registration has MANY players.
registration.ID = players.registrationID
my link goes to players/index®istrationID=29
Gridview shows up, but none of the results are filtering to only show players with a registrationID of 29.
actionIndex
public function actionIndex()
{
$searchModel = new PlayersSearch();
if (isset($_GET['Players']))
$searchModel->attributes = $_GET['Players'];
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
actionList (also tried linking to players/list®istrationID=29):
public function actionList()
{
$model = new PlayersSearch();
$model->unsetAttributes(); // clear default vals
if (isset($_GET['Players'])) {
$model->attributes = $_GET['Players'];
}
$criteria = new CDbCriteria;
$criteria->order = 'ID ASC';
//regardless of the filter always apply this criteria
$model->CDbCriteria = $criteria;
return $this->render('index',[
'model' => $model]);
}
players/index.php Gridview widget:
<?php Pjax::begin(); ?> <?= GridView::widget([
'model' => $model,
'filterModel' => $model,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'ID',
'registrationID',
'fullName',
'phone',
'email:email',
'city',
'state',
// 'street',
// 'zip',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
PlayersSearch model:
public function search($params)
{
$query = players::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
// setup sorting attributes BEFoRE $this->load($params)
$dataProvider->setSort([
'attributes' =>[
'ID',
'registrationID',
'fullName' => [
'asc' => [
'first' => SORT_ASC,
'last' => SORT_ASC],
'desc' => [
'first' => SORT_DESC,
'last' => SORT_DESC],
'label' => 'Full Name',
'default' => SORT_ASC
],
'email',
'phone',
'city',
'state',
]
]);
if (isset($_GET['PlayersSearch']) && !($this->load($params) && $this->validate())) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
$query->joinWith(['registration']);
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere(['ID' => $this->ID]);
$query->andFilterWhere(['registrationID' => $this->registrationID]);
$query->andFilterWhere(['first' => $this->first]);
$query->andFilterWhere(['last' => $this->last]);
$query->andFilterWhere(['email' => $this->email]);
$query->andFilterWhere(['phone' => $this->phone]);
$query->andFilterWhere(['city' => $this->city]);
$query->andFilterWhere(['state' => $this->state]);
$query->andWhere('first LIKE "%' . $this->fullName . '%" ' . 'OR last LIKE "%' . $this->fullName . '%"');
return $dataProvider;
}
PLEASE help me figure out what I’m doing wrong. I could do it in Yii 1, but don’t know how in Yii2.