YII2 Permanently Filtered Index List

This seems so simple, even to me but I am going in circles. After 3rd day of errors I’m finally going to ask…

I’ve generated CRUD and everything works fine.

The users table is the same table that all people objects in system are stored. Everyone can login.

When anyone logs in I need them to only see users where parentID = their ID AND isActive = 1.

This is the Active Record code I need to add BUT WHERE? It’s driving me crazy. I don’t see anything that looks like this on any model or controller.

PK in user table db1_user is ‘id’.




$db1_user = user::find()

    ->where(['parentID' => Yii::$app->user->identity->id])

    ->where(['isActive' => '1'])

    ->orderBy('created_at')

    ->all();

How do I get the above to work with actionIndex below: :huh: ???


public function actionIndex()

    {    

        $searchModel = new UserSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }

As you can tell I’m very new to PHP. Any clues about where this code goes would be awesome.

Ignore all of the models and controllers like client, subclient, attorney… I duplicate the user table and then created crud for all of those but they will all end up working off of only the db1_user table.

Sorry I’m not allowed to embed because I’m still a shoobie (surf lexicon for “noob”).

http REMOVE ://screencast.com/t/FSrV3m76

You can’t chain the where() calls like that. You need to set it all at once, or chain using andWhere() orWhere()

For a simple case like yours, you can do




->where([

    'parentID' => Yii::$app->user->identity->id],

    'isActive' => '1'

])



Edit: full details here http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html#where

It finally clicked. I see where to put that code now. Now I can move forward with my project not using SQL everywhere. The last time I dabbled with this stuff, Allaire still owned ColdFusion and you just put all the SQL and HTML all on the same page. This framework stuff is really neat.

Thanks bunches.

CR