I have created like this,
cnotroller
class UpdateController extends \yii\web\Controller
{
public function actionIndex()
{
$homeaddress = new HomeAddress();
$address = new Address();
if ($homeaddress->load(Yii::$app->request->post()) && $address->load(Yii::$app->request->post())){
$toSave = $homeaddress->toSave();
$toSave = $address->toSave();
if($toSave){
$homeaddress->save(false);
$address->save(false);
return $this->render('index',['homeaddress' => $homeaddress,'address'=>$address]);
}
}
}
models are like this
class HomeAddress extends \yii\mongodb\ActiveRecord
{
/**
* @inheritdoc
*/
public static function collectionName()
{
return ['user_information', 'Information'];
}
/**
* @inheritdoc
*/
public function attributes()
{
return [
'_id',
'name',
'mobile',
'email',
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name','mobile','email'], 'safe']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'_id' => 'ID',
'name' => 'Name',
'mobile' => 'Mobile',
'email' => 'Email',
];
}
public function toSave()
{
if($this->validate()){
$homeaddress = new HomeAddress();
$homeaddress->name= $this->name;
$homeaddress->mobile= $this->mobile;
$homeaddress->email= $this->email;
$homeaddress->save();
return true;
}else{
return false;
}
}
}
class Address extends \yii\mongodb\ActiveRecord
{
/**
* @inheritdoc
*/
public static function collectionName()
{
return ['user_information', 'Address'];
}
/**
* @inheritdoc
*/
public function attributes()
{
return [
'_id',
'address1',
'address2',
'city',
'state',
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['address1', 'address2', 'city', 'state'], 'safe']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'_id' => 'ID',
'address1' => 'Address1',
'address2' => 'Address2',
'city' => 'City',
'state' => 'State',
];
}
public function toSave()
{
if($this->validate()){
$address = new Address();
$address->address1= $this->address1;
$address->address2= $this->address2;
$address->city= $this->city;
$address->state= $this->state;
$address->save();
return true;
}else{
return false;
}
}
}
But after this when i run the controller action it shows blank on browser,It doesn’t going to models.