Using Single Form To Insert The Data Into Two Tables

Hi folks,

      I am newbie for yii, i have two tables like customer,customer detail 

In customer (customer_id,cutomer_name,email,…etc)

In customerdetail(customerdetail_id,customer_id,builder_id,…etc).I am using single form for both tables.

the data are stored in first table but not stored in second table,how can i solve this problem,please any one suggest me.I am getting this kind of error

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (myveedudb.myv_customerdetail, CONSTRAINT myv_customerdetail_ibfk_1 FOREIGN KEY (myv_customer_id) REFERENCES myv_customer (myv_Customer_id) ON DELETE CASCADE ON UPDATE CASCADE). The SQL statement executed was: INSERT INTO myv_customerdetail (myv_builder_id, myv_project_id, myv_project_block_id, myv_project_block_floor_id, myv_apartment_id, myv_customer_booked_on, myv_customer_crm_id) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6)

Here is my code

Controller:

public function actionCreate()

{


	$model=new MyvCustomer;


            $model2=new MyvCustomerdetail;





	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);





	if(isset($_POST['MyvCustomer'],$_POST['MyvCustomerdetail']))


	{


                


               


		$model->attributes=$_POST['MyvCustomer'];


                    $model2->attributes=$_POST['MyvCustomerdetail'];


                     $valid=$model->validate();


                     $valid=$model2->validate() && $valid;


		


                   


                 


                    if($valid){


                        


                       $model->save(false);


                       $model2->save(false);


                     }


	}





	$this->render('create',array(


		'model'=>$model,


                    'model2'=>$model2, 


	));


}

customer_id is generated when you save, try this:




if($valid){

  $model->save(false);

  $model2->customer_id=$model->customer_id;

  $model2->save(false);

}



This will help you a lot.




public function actionCreate()

{

    $a=new A;

    $b=new B;

    if(isset($_POST['A'], $_POST['B']))

    {

        // populate input data to $a and $b

        $a->attributes=$_POST['A'];

        $b->attributes=$_POST['B'];

 

        // validate BOTH $a and $b

        $valid=$a->validate();

        $valid=$b->validate() && $valid;

 

        if($valid)

        {

            // use false parameter to disable validation

            $a->save(false);

            $b->save(false);

            // ...redirect to another page

        }

    }

 

    $this->render('create', array(

        'a'=>$a,

        'b'=>$b,

    ));

}






I am using this but i getting this error "Property "customer_id" is not defined. "

Yes i follow these steps but data not inserted in second table, In first table (cutomer_id,customer_image,email…etc) in second table(customer_detail_id,cutomer_id,…etc)

this is my controller code

public function actionCreate()

{


	$model=new MyvCustomer;


            $model2=new MyvCustomerdetail;





	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);





	if(isset($_POST['MyvCustomer'],$_POST['MyvCustomerdetail']))


	{


                $rnd = rand(0,9999);  // generate random number between 0-9999


                $uploadedFile=CUploadedFile::getInstance($model,'myv_Customer_profileimage');


                


               $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name


               $model->myv_Customer_profileimage = $fileName;


               


		$model->attributes=$_POST['MyvCustomer'];


                    $model2->attributes=$_POST['MyvCustomerdetail'];


                     $valid=$model->validate();


                     $valid=$model2->validate() && $valid;

// if($valid){

// $model->save(false);

// $uploadedFile->saveAs(Yii::app()->basePath.’/../images/customerprofileimage/’.$fileName); // image

// $model2->save(false);

// $this->redirect(array(‘view’,‘id’=>$model->myv_Customer_id));

// }

// if($valid){

// if($model->save())

// {

// $uploadedFile->saveAs(Yii::app()->basePath.’/../images/customerprofileimage/’.$fileName); // image

// $this->redirect(array(‘view’,‘id’=>$model->myv_Customer_id));

// }

// if($model2->save())

// {

// //$uploadedFile->saveAs(Yii::app()->basePath.’/../images/builderimage/’.$fileName); // image

// $model2->myv_customer_id=$model->myv_customer_id;

//

// $this->redirect(array(‘view’,‘id’=>$model2->myv_customerdetail_id));

// }

// }

                     if($valid){


                         


                         $model->save(false);


                         $model2->myv_customer_id=$model->myv_customer_id;


                         $model2->save(false);


                         $this->redirect(array('view','id'=>$model->myv_Customer_id));


                     }


	}





	$this->render('create',array(


		'model'=>$model,


                    


                    'model2'=>$model2, 


	));


}

Sorry, I dont know the name of the fields but you have to create the relation between 2 tables.

That relation is $model2->FK=$model->PK;

FK = FOREIGN KEY

PK = PRIMARY KEY

I believe PK = myv_customer_id;

Fk is one of (myv_builder_id, myv_project_id, myv_project_block_id, myv_project_block_floor_id, myv_apartment_id, myv_customer_booked_on, myv_customer_crm_id)

you have to create the relationship for each foreign key.

if you want to see the parameters (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6) in config/main.php, in db component:

‘enableParamLogging’ => true,

This is the two table fields

myv_customer (

myv_Customer_id int(11) unsigned NOT NULL AUTO_INCREMENT,

myv_Customer_firstname varchar(60) CHARACTER SET utf8 NOT NULL,

myv_Customer_lastname varchar(60) CHARACTER SET utf8 NOT NULL,

myv_Customer_dob date NOT NULL,

myv_Customer_doa date DEFAULT NULL,

myv_Customer_address1 tinytext CHARACTER SET utf8 NOT NULL,

myv_Customer_address2 tinytext CHARACTER SET utf8,

myv_locality_id int(11) unsigned NOT NULL,

myv_city_id int(11) unsigned NOT NULL,

myv_state_id int(11) unsigned NOT NULL,

myv_customer_pincode int(11) unsigned NOT NULL,

myv_Customer_landmark1 varchar(100) CHARACTER SET utf8 NOT NULL,

myv_Customer_landmark2 varchar(100) CHARACTER SET utf8 DEFAULT NULL,

myv_Customer_callback_no bigint(12) NOT NULL,

myv_Customer_alternate_no bigint(12) DEFAULT NULL,

myv_Customer_email varchar(60) CHARACTER SET utf8 NOT NULL,

myv_Customer_profileimage varchar(100) CHARACTER SET utf8 NOT NULL,

myv_customer_createddate datetime NOT NULL,

PRIMARY KEY (myv_Customer_id),

KEY myv_locality_id (myv_locality_id,myv_city_id,myv_state_id),

KEY myv_city_id (myv_city_id),

KEY myv_state_id (myv_state_id)

second table field

myv_customerdetail (

myv_customerdetail_id int(11) unsigned NOT NULL AUTO_INCREMENT,

myv_customer_id int(11) unsigned NOT NULL,

myv_builder_id int(11) unsigned NOT NULL,

myv_project_id int(11) unsigned NOT NULL,

myv_project_block_id int(11) unsigned NOT NULL,

myv_project_block_floor_id int(11) unsigned NOT NULL,

myv_apartment_id int(11) unsigned NOT NULL,

myv_customer_booked_on date NOT NULL,

myv_customer_crm_id int(11) unsigned NOT NULL,

PRIMARY KEY (myv_customerdetail_id),

KEY myv_customer_id (myv_customer_id,myv_builder_id,myv_project_id,myv_project_block_id,myv_project_block_floor_id,myv_apartment_id),

KEY myv_customer_id_2 (myv_customer_id),

KEY myv_builder_id (myv_builder_id),

KEY myv_project_id (myv_project_id),

KEY myv_project_block_id (myv_project_block_id),

KEY myv_project_block_floor_id (myv_project_block_floor_id),

KEY myv_apartment_id (myv_apartment_id),

KEY myv_customerdetail_ibfk_7 (myv_customer_crm_id)

you can have several foreign key

Insert the sql in your database manager.

INSERT INTO myv_customerdetail (myv_customer_id,myv_builder_id, myv_project_id, myv_project_block_id, myv_project_block_floor_id, myv_apartment_id, myv_customer_booked_on, myv_customer_crm_id)

VALUES (‘x’,’’,’’,’’,’’,’’,‘x’,’’)

myv_builder_id is the value of the first table.

myv_customer_booked_on is date,

try to insert and you’ll get errors, and you’ll discover foreign key. (one or more)

when the sql is ok, compare with the sql generated by yii