Misko Mali
Misko Mali

Reputation: 619

How to access module's model in yii2

I have installed dektrium's yii2-user. I saw there are few models like User, UserSearch... I would like to access those models and its functions anywhere in my code and my controllers. But not sure how to do it.

I know how to call module, but not sure how to call functions within module's models. This is how I fetch module.

Yii::getModule('user')

Upvotes: 0

Views: 2473

Answers (2)

Bornwell Matembudze
Bornwell Matembudze

Reputation: 1

In the file that you want to access it from include the following example code use dektrium\user\models\profile; then use it like this

<?= $form->field($model, 'client')->dropDownList(ArrayHelper::map(Profile::find()->all(),'user_id','name'))

Change the model needed accordingly

Upvotes: 0

Ali MasudianPour
Ali MasudianPour

Reputation: 14459

You can use models globally like below:

use app\models\ModelName;
$model=new ModelName();
//rest of code

Or

use app\models\ModelName;
ModelName::find()->all(); //for example

The key note is that, you need to use correct namespace. For example app\modules\moduleName\models

Upvotes: 3

Related Questions