Hi, please come to my rescue. I have tried this for 2 weeks now and I seem not to get the solution.
I have user table and an Expense table
I have a relationship btwn these table via id in user table and user_id in expense table.
In my ExpenseSearch Model I have
namespace frontend\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use frontend\models\Expense;
/**
* ExpenseSearch represents the model behind the search form of `frontend\models\Expense`.
*/
class ExpenseSearch extends Expense
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'user_id', 'trans_ref', 'expense_amount'], 'integer'],
[['description', 'expense_date', 'payment_method'], '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
* @param string|null $formName Form name to be used into `->load()` method.
*
* @return ActiveDataProvider
*/
public function search($params, $formName = null)
{
$query = Expense::find()->where(['user_id' => Yii::$app->user->identity->id]);;
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params, $formName);
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,
'user_id' => $this->user_id,
'trans_ref' => $this->trans_ref,
'expense_amount' => $this->expense_amount,
'expense_date' => $this->expense_date,
]);
$query->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'payment_method', $this->payment_method]);
return $dataProvider;
}
}
Index is as below
public function actionIndex()
{
$searchModel = new ExpenseSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Where could I be going wrong.
Thanks in advance
Where could I be going wrong.
Thanks in advance