Yii Relations in view file

Hello everyone,

I have Cinema table & City table and I have relation with this tables by id.

I need to show city name by city_id from city table in view file… how can me do that ?

Cinema model


public function relations()

{

    // NOTE: you may need to adjust the relation name and the related

    // class name for the relations automatically generated below.

    return array(

        'countryCode' => array(self::BELONGS_TO, 'TblCountry', 'country_code'),

        'city' => array(self::BELONGS_TO, 'TblCity', 'city_id'),

        'tblCinemaMovies' => array(self::HAS_MANY, 'TblCinemaMovies', 'cinema_id'),

        'tblDays' => array(self::HAS_MANY, 'TblDay', 'cinema_id'),

        'tblShowtimes' => array(self::HAS_MANY, 'TblShowtime', 'cinema_id'),

    );

}

City model


public function relations()

{

    // NOTE: you may need to adjust the relation name and the related

    // class name for the relations automatically generated below.

    return array(

        'tblCinemas' => array(self::HAS_MANY, 'TblCinema', 'city_id'),

        'countryCode' => array(self::BELONGS_TO, 'TblCountry', 'country_code'),

    );

}

database:

cinema:

2709

cinema_db.jpg

city:

2710

city_db.jpg

Hi,

I’m not going to tell you how exactly you can do the job cause your requirement is so general and easy to solve by yourself if you study yii or you have asked google a bit.

http://www.yiiframew…en/database.arr

http://www.yiiframew…l-in-cgridview/

http://www.yiiframew…related-models/

If you get stuck at some point or get some errors you can’t solve you can always ask here in the forum again (with related code) and for sure someone is going to help you.