Please help,
[This is my first Yii Project]
-
I have two database db1 and db2
-
There are two tables : ‘member’ in db1 and ‘userinfo’ in db2
-
My registration form successfully connect to table ‘member’ in db1
My question : How to save records from single form into two tables ‘member’ in db1 and ‘userinfo’ in db2?
Please give me steps to do or maybe some code to explain, thank you very much.
perochak
(Amjad Mughal)
2
Welcome to Yii Family
You can do this is
Controller action
$userinfo= new Users;
$member= new Members;
if($_POST){
$usersinfo->attributes=$_POST['Userinfo'];
$member->attributes=$_POST['Members'];
if($userinf->validate() && $member->validate()){
$member->save();
$userinfo->save();
}
}
thanks
yugenekr
(Yugenekr)
3
Hi, pepsikaleng
[size=“2”]I suppose you’re using AR? I’d suggest to follow these basic steps [/size]
-
Configure one more db connection in your configuration file, for example, under db2 name (usually I use name that describes db but is still short)
-
Create parent AR class for all your tables/classes from the second db. You may put it under /components
You’ll need to have something like:
class DB2ActiveRecord extends GxActiveRecord{
public static $db2;
public function getDbConnection(){
if(self::$db2 !== null){
return self::$db2;
}else{
self::$db2 = Yii::app()->db2;
if (self::$db2 instanceof CDbConnection) {
self::$db2->setActive(true);
return self::$db2;
}else{
throw new CDbException('Active Record requires a "db2" CDbConnection application component.');
}
}
}
}
-
Extend db2 classes from relevant parent. You may put them into separate folder if there’re many of them.
-
tableName() function for these classes should contain db name in returned string, for example:
public function tableName()
{
return 'db2.mytable';
}
And then follow what is Perochak wrote.
Hope I didn’t forget anything, let me know if it’s so