yii2-mongodb: How i can make the has many or has one relationship between two collection -
i using 2 collection state , city , want relationship between both of them , using below code these:
common\models\state.php
return $this->hasmany(city::classname(), ['state_id'=>'_id']);
common\models\city.php
return $this->hasone(state::classname(), ['_id' =>'state_id']);
when run below code, running , return blank result cities. $model = \common\models\state::find()->all();
when change state_id type in collection objectid returns result. please suggest me how can typecast state_id in relation.
for example; comment associated single user, in comment class
public function getuser() { return $this->hasone(user::classname(), ['_id' => 'user_id']); }
in controller want retrieve comments , user objects
$query = comment::find()->orderby('created_at desc'); $dataprovider = new activedataprovider([ 'pagination' => ['pagesize' =>5], 'query' => $query, ]); return $this->render('index', [ 'dataprovider' => $dataprovider, ]);
you can access user objects comments view
$model->user['profile_url']
Comments
Post a Comment