I have two tables and i want to make them as one table using mongoDb + yii2. How can i do that?
Below i have attached screenshot of those table.
I have two tables and i want to make them as one table using mongoDb + yii2. How can i do that?
Below i have attached screenshot of those table.
I’m not familiar with mongoDB, so please correct me if I’m wrong.
Hemanth is trying to relate the 2 models (or collections), user and address.
Collection 1 user: id, name, mobile, email
Collection 2 address: id, address1, address2, city, state
A user has one address? Or A user has many addresses? I’m not sure. Anyway, he wants to establish the relationship between the two.
In mongoDB, there are 2 ways to establish relationship: references and embedded documents.
https://docs.mongodb.org/manual/core/data-modeling-introduction/
But, yii\mongodb\ActiveRecord doesn’t have a support for embedded documents, at least at the moment.
So, we have to use references. Either by adding "address_id" in "user" collection or adding "user_id" in "address" collection, we may be able to relate the two, … just like we do with foreign keys in RDB.
I have joined the two tables according to your guide and using this
public function getAddress()
{
return $this->hasOne(Address::className(), ['info_id' => '_id']);
}
in model and
this
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'_id',
'name',
'mobile',
'email',
'address.address1',
'address.address2',
'address.city',
'address.state',
//'address.info_id',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
in view.
Now when i click on view button it shows documents of both collections but when i click on update button it shows only ‘information’ collection documents only, remaining fields are showing blank.
Any idea about this?
If you want to create/update Info and Address together in a controller, you have to use the 2 models.
http://www.yiiframework.com/doc-2.0/guide-input-multiple-models.html