php - How to Access yii2 relation map table fields -


i have 3 table names 'agency' , 'property' , 'property_agency_map'

agency table :

-------------------------- | id => primary key ...  | | name => string ...     | | , som more fields    | -------------------------- 

property table :

-------------------------- | id => primary key ...  | | state => string ...    | | adress => string ...   | | , som more fields    | -------------------------- 

property_agency_map table :

------------------------------ | id => primary key ...      | | agency_id => string ...    | | property_id => string ...  | | status =>  intiger ...     | | favorite =>  intiger ...   | ------------------------------ 

the property_agency_map table connect agency table property table.

agency model :

 public function getproperties(){     return $this->hasmany(property::classname(),['id'=>'property_id'])         ->viatable('"{{%property_agency_map}}"',['agency_id'=>'id']); } 

now question how access 'favorite' , 'status' field in property_agency_map ??

you should create new method property_agency_map items agency:

namespace app\models;  use yii\db\activerecord;  class propertyagencymap extends activerecord {     /**      * @return string name of table associated activerecord class.      */     public static function tablename()     {         return 'property_agency_map';     } }  class agency extends activerecord {     /**      * @return string name of table associated activerecord class.      */     public static function tablename()     {         return 'agency';     }      /**      * @return \yii\db\activequery      */     public function getpropertyagency()     {         return $this->hasmany(propertyagencymap::classname(), ['agency_id' => 'id']);     } } 

Comments