How to make relationship with other model using module

Hi All,

I am new to Yii2. I am stuck in creating relationship using module

I am in a module name surveyOwner in which there are different models.One of them is vendor module.

Vendor module has an attribute CityId which is related to City model which is saved in backend\models.

While displaying Vendor I want to display CityName instead of CityId.

My code is

[color="#FF8C00"]Vendor Model:[/color]




<?php


namespace backend\modules\surveyOwner\models;


use Yii;


/**

 * This is the model class for table "vendor".

 *

 * @property integer $VendorId

 * @property integer $SurveyOwnerId

 * @property string $VendorCode

 * @property string $VendorName

 * @property string $VendorPassword

 * @property string $VendorRandomNo

 * @property string $VendorEmail

 * @property string $VendorMobile

 * @property string $VendorLandline

 * @property string $Street1

 * @property string $Street2

 * @property string $Landmark

 * @property string $Zipcode

 * @property integer $CityId

 * @property integer $StateId

 * @property integer $CountryId

 * @property string $VendorImage

 * @property integer $VendorStatus

 * @property string $CreatedOn

 * @property string $ModifiedOn

 *

 * @property Surveyor[] $surveyors

 * @property Surveyvendor[] $surveyvendors

 * @property Surveyowner $surveyOwner

 * @property City $city

 * @property State $state

 * @property Country $country

 * @property Vendordocument[] $vendordocuments

 */

class Vendor extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'vendor';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['SurveyOwnerId', 'VendorCode', 'VendorName', 'VendorPassword','VendorEmail', 'VendorMobile', 'VendorLandline', 'Street1', 'Street2', 'Landmark', 'Zipcode', 'CityId', 'StateId', 'CountryId', 'VendorImage', 'VendorStatus'], 'required'],

            [['SurveyOwnerId', 'CityId', 'StateId', 'CountryId', 'VendorStatus'], 'integer'],

            [['CreatedOn', 'ModifiedOn'], 'safe'],

            [['VendorCode', 'VendorRandomNo'], 'string', 'max' => 20],

            [['VendorName', 'VendorPassword', 'VendorEmail'], 'string', 'max' => 100],

            [['VendorMobile', 'VendorLandline'], 'string', 'max' => 15],

            [['Street1', 'Street2'], 'string', 'max' => 40],

            [['Landmark'], 'string', 'max' => 80],

            [['Zipcode'], 'string', 'max' => 10],

            [['VendorImage'], 'string', 'max' => 200]

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'VendorId' => 'Vendor ID',

            'SurveyOwnerId' => 'Survey Owner ID',

            'VendorCode' => 'Vendor Code',

            'VendorName' => 'Name',

            'VendorPassword' => 'Vendor Password',

            'VendorRandomNo' => 'Vendor Random No',

            'VendorEmail' => 'Email',

            'VendorMobile' => 'Mobile',

            'VendorLandline' => 'Landline',

            'Street1' => 'Street1',

            'Street2' => 'Street2',

            'Landmark' => 'Landmark',

            'Zipcode' => 'Zipcode',

            'CityId' => 'City',

            'StateId' => 'State',

            'CountryId' => 'Country',

            'VendorImage' => 'Image',

            'VendorStatus' => 'Status',

            'CreatedOn' => 'Created On',

            'ModifiedOn' => 'Modified On',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getSurveyors()

    {

        return $this->hasMany(Surveyor::className(), ['VendorId' => 'VendorId']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getSurveyvendors()

    {

        return $this->hasMany(Surveyvendor::className(), ['VendorId' => 'VendorId']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getSurveyOwner()

    {

        return $this->hasOne(Surveyowner::className(), ['SurveyOwnerId' => 'SurveyOwnerId']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getCity()

    {

        return $this->hasOne(City::className(), ['CityId' => 'CityId']);

//        return $this->hasMany(City::className(), ['CityId' => 'CityId']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getState()

    {

        return $this->hasOne(State::className(), ['StateId' => 'StateId']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getCountry()

    {

        return $this->hasOne(Country::className(), ['CountryId' => 'CountryId']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getVendordocuments()

    {

        return $this->hasMany(Vendordocument::className(), ['VendorId' => 'VendorId']);

    }

}



My View is:




<?php


use yii\helpers\Html;

use yii\widgets\DetailView;

use app\models\City;

//use backend\models\City;

//use app\models\Country;




/* @var $this yii\web\View */

/* @var $model backend\modules\surveyOwner\models\Vendor */


$this->title = $model->VendorName;

$this->params['breadcrumbs'][] = ['label' => 'Vendors', 'url' => ['index']];

$this->params['breadcrumbs'][] = $this->title;

?>

<div class="vendor-view">


   

    <p>

        <?= Html::a('Update', ['update', 'id' => $model->VendorId], ['class' => 'btn btn-primary']) ?>

        <?= Html::a('Delete', ['delete', 'id' => $model->VendorId], [

            'class' => 'btn btn-danger',

            'data' => [

                'confirm' => 'Are you sure you want to delete this item?',

                'method' => 'post',

            ],

        ]) ?>

    </p>


    <?= DetailView::widget([

        'model' => $model,

        'attributes' => [

            'VendorId',

            'SurveyOwnerId',

            'VendorCode',

            'VendorName',

            'VendorEmail:email',

            'VendorMobile',

            'VendorLandline',

            'Street1',

            'Street2',

            'Landmark',

            'Zipcode',

            'CityId',

            'city.CityName',

            'StateId',

            'CountryId',

            'VendorImage',

            'VendorStatus',

            'CreatedOn:date',

        ],

    ]) ?>


</div>



Just use the namespace of City model.




use backend\models\City; // or something

...

    /**

     * @return \yii\db\ActiveQuery

     */

    public function getCity()

    {

        return $this->hasOne(City::className(), ['CityId' => 'CityId']);

//        return $this->hasMany(City::className(), ['CityId' => 'CityId']);

    }



If you share the model both in backend and frontend, you’d be better move it under ‘common\models’.

Thanxxxxx