Error during Insertion

I have two models: (1)Message and (2)Messageto

During data insertion: at first the records should passed into first table then pass to the next only when the first records are inserted. Other wise there will be roll back system.





                $modelMsg = new Message;

		$modelMsgToX = new Messageto;

		

		if(isset($_POST['Message']))

		{

			$modelMsg->messageSubject = $_POST['Message']['messageSubject'];

			$modelMsg->messageText = $_POST['Message']['messageText'];

			$modelMsg->toAddress = $_POST['Message']['authorId']; 	// to			

			$modelMsg->threadOf = $_POST['Message']['messageId'];				

			$modelMsg->authorId = Yii::app()->user->id;

			$modelMsg->messageTime = new CDbExpression('NOW()');				

			// access current IP address

			$modelMsg->authorIP = Yii::app()->request->userHostAddress;				

						

			/**

		     * Update a field isReplied.

		     * If a message is replied by curent user the previous record field 'isReplied' status

		     * is changed to 1.

		     */				

			$transaction = $modelMsg->dbConnection->beginTransaction();			

			try{

                 // STEP 1

				if($modelMsg->save())

				{					

					// message send to receiver

					$modelMsgToX->messageId = $modelMsg->messageId;

					$modelMsgToX->authorId = $modelMsg->authorId;	// from

					$modelMsgToX->memberId = $modelMsg->toAddress;	// to

					$modelMsgToX->isNew =1;

                                     	$modelMsgToX->messageStatusId =1;

					

                    // STEP 2                    

					if($modelMsgToX->save())					

					{

						 //stores a copy of record to sender

						$modelMsgTo->isNewRecord = true;						

						$modelMsgToX->memberId =$modelMsg->authorId;

						$modelMsgToX->isNew =0;						

						$modelMsgToX->messageStatusId =2;


                         // STEP 3

						$modelMsgToX->save();						

					}

					else

					{

						throw new CException($modelMsgToX->getErrors());

					}

				}

				else

				{

					throw new CException($modelMsg->getErrors());

				}

				$transaction->commit();						

			}

			catch(CException $er){

				$transaction->rollBack();

			}			

		}



Record is inserted in STEP 1 and STEP 3 but does not insert in STEP 2. I am so amaze. Without completing the process 2 how can a process jump to STEP3??

Can any one give me hint??

On the 3 step… you are not creating a new $modelMsgToX (MessageTo) model… so it just updates the step2 record…

To test this you can just comment the step 3 and see if the step 2 gets saved…

To solve it you can add to step 3


$modelMsgToX= new MessageTo;

or you can just use


$modelMsgToX->isNewRecord=true;

When I commented the STEP3; records were passed to the database. But when I again tried to insert the records, records couldn’t be inserted . Even though I made :


$modelMsgToX->isNewRecord=true;	

desire result could not be get.

Here is the application log file:





[10:57:21.292][trace][system.db.CDbCommand] Querying SQL: SHOW COLUMNS FROM `tbl_message` in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (53) in E:\xampp\htdocs\socialsite\index.php (13)

[10:57:21.296][trace][system.db.CDbCommand] Querying SQL: SHOW CREATE TABLE `tbl_message` in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (53) in E:\xampp\htdocs\socialsite\index.php (13)

[10:57:21.297][trace][system.db.CDbCommand] Querying SQL: SHOW COLUMNS FROM `tbl_messageto` in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (54) in E:\xampp\htdocs\socialsite\index.php (13)

[10:57:21.300][trace][system.db.CDbCommand] Querying SQL: SHOW CREATE TABLE `tbl_messageto` in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (54) in E:\xampp\htdocs\socialsite\index.php (13)

[10:57:21.307][trace][system.db.CDbCommand] Executing SQL: INSERT INTO `tbl_message` (`messageSubject`, `messageText`, `toAddress`, `threadOf`, `authorId`, `messageTime`, `authorIP`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, NOW(), :yp5). Bound with :yp0='Message from Ishwor to Humble-1', :yp1='Hello Ishwor HOw areu', :yp2='8', :yp3=97, :yp4=7, :yp5='::1' in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (74) in E:\xampp\htdocs\socialsite\index.php (13)

[10:57:21.309][trace][system.db.CDbCommand] Executing SQL: INSERT INTO `tbl_messageto` (`messageId`, `authorId`, `memberId`, `isNew`, `isUnread`, `isReplied`, `messageStatusId`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6). Bound with :yp0=178, :yp1=7, :yp2=8, :yp3=1, :yp4=1, :yp5=1, :yp6=1 in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (86) in E:\xampp\htdocs\socialsite\index.php (13)

[10:57:21.310][trace][system.db.CDbCommand] Executing SQL: INSERT INTO `tbl_messageto` (`messageId`, `authorId`, `memberId`, `isNew`, `isUnread`, `isReplied`, `messageStatusId`, `id`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7). Bound with :yp0=178, :yp1=7, :yp2=7, :yp3=0, :yp4=0, :yp5=0, :yp6=2, :yp7=43 in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (96) in E:\xampp\htdocs\socialsite\index.php (13)

[10:57:21.310][error][system.db.CDbCommand] Error in executing SQL: INSERT INTO `tbl_messageto` (`messageId`, `authorId`, `memberId`, `isNew`, `isUnread`, `isReplied`, `messageStatusId`, `id`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7). Bound with :yp0=178, :yp1=7, :yp2=7, :yp3=0, :yp4=0, :yp5=0, :yp6=2, :yp7=43 in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (96) in E:\xampp\htdocs\socialsite\index.php (13)

index.php?r=message/message/view&id=97()index....w&id=97 (line 129)

[Break On This Error] console.error("[10:57:21.310][error][...htdocs\\socialsite\\index.php (13)");

index....w&id=97 (line 129)

[10:57:21.336][trace][system.db.CDbCommand] Querying SQL: SELECT * FROM `tbl_message` `t` WHERE `t`.`messageId`=97 LIMIT 1 in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (318) in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (134) in E:\xampp\htdocs\socialsite\index.php (13)

[10:57:21.345][trace][system.db.CDbCommand] Querying SQL: SELECT M.*,MT.* FROM tbl_message AS M, tbl_messageto AS MT WHERE (M.messageId ='97' OR M.threadOf ='97') AND (MT.messageStatusId=1 AND M.messageId = MT.messageId) ORDER BY M.messageTime ASC LIMIT 10 in E:\xampp\htdocs\socialsite\protected\modules\message\views\message\view.php (36) in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (138) in E:\xampp\htdocs\socialsite\index.php (13)

[10:57:21.347][trace][system.db.CDbCommand] Querying SQL: SHOW COLUMNS FROM `tbl_profileimage` in E:\xampp\htdocs\socialsite\protected\modules\message\models\Profileimage.php (19) in E:\xampp\htdocs\socialsite\protected\modules\message\models\Profileimage.php (96) in E:\xampp\htdocs\socialsite\protected\modules\message\views\message\view.php (37)

[10:57:21.349][trace][system.db.CDbCommand] Querying SQL: SHOW CREATE TABLE `tbl_profileimage` in E:\xampp\htdocs\socialsite\protected\modules\message\models\Profileimage.php (19) in E:\xampp\htdocs\socialsite\protected\modules\message\models\Profileimage.php (96) in E:\xampp\htdocs\socialsite\protected\modules\message\views\message\view.php (37)

[10:57:21.350][trace][system.db.CDbCommand] Querying SQL: SELECT * FROM `tbl_profileimage` `t` WHERE `t`.`memberId`=:yp0 LIMIT 1. Bound with :yp0='8' in E:\xampp\htdocs\socialsite\protected\modules\message\models\Profileimage.php (96) in E:\xampp\htdocs\socialsite\protected\modules\message\views\message\view.php (37) in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (138)

[10:57:21.352][trace][system.db.CDbCommand] Querying SQL: SHOW COLUMNS FROM `tbl_member` in E:\xampp\htdocs\socialsite\protected\modules\message\models\Member.php (27) in E:\xampp\htdocs\socialsite\protected\modules\message\models\Message.php (119) in E:\xampp\htdocs\socialsite\protected\modules\message\views\message\view.php (37)

[10:57:21.354][trace][system.db.CDbCommand] Querying SQL: SHOW CREATE TABLE `tbl_member` in E:\xampp\htdocs\socialsite\protected\modules\message\models\Member.php (27) in E:\xampp\htdocs\socialsite\protected\modules\message\models\Message.php (119) in E:\xampp\htdocs\socialsite\protected\modules\message\views\message\view.php (37)

[10:57:21.355][trace][system.db.CDbCommand] Querying SQL: SELECT * FROM `tbl_member` `t` WHERE `t`.`id`=8 LIMIT 1 in E:\xampp\htdocs\socialsite\protected\modules\message\models\Message.php (119) in E:\xampp\htdocs\socialsite\protected\modules\message\views\message\view.php (37) in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (138)

[10:57:21.360][trace][system.db.CDbCommand] Querying SQL: SELECT * FROM `tbl_profileimage` `t` WHERE `t`.`memberId`=:yp0 LIMIT 1. Bound with :yp0='7' in E:\xampp\htdocs\socialsite\protected\modules\message\models\Profileimage.php (96) in E:\xampp\htdocs\socialsite\protected\modules\message\views\message\view.php (37) in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (138)

[10:57:21.361][trace][system.db.CDbCommand] Querying SQL: SELECT * FROM `tbl_member` `t` WHERE `t`.`id`=7 LIMIT 1 in E:\xampp\htdocs\socialsite\protected\modules\message\models\Message.php (119) in E:\xampp\htdocs\socialsite\protected\modules\message\views\message\view.php (37) in E:\xampp\htdocs\socialsite\protected\modules\message\controllers\MessageController.php (138)



As you can see in the log… the STEP 3 is now doing an INSERT… not an UPDATE…

but it’s producing an error… do you get the error message?

I noticed one thing…

the first INSERT to tbl_messageto (STEP 2) has 7 parameters… the second INSERT (STEP3) has 8 parameters… but from the code above I don’t see how and why… could be that the problem…

I noticed one error or ‘is that error or not I am not sure’, to insert second time in the same table I increased the primary id of that table by 1. After that the record was inserted.

The modified code is :




....

....


// STEP 2                    

if($modelMsgToX->save())                                        

{

	//stores a copy of record to sender

        $modelMsgTo->isNewRecord = true;                                                

        $modelMsgToX->memberId =$modelMsg->authorId;

        $modelMsgToX->isNew =0;                                         

        $modelMsgToX->messageStatusId =2;


         // STEP 3

         $modelMsgToX->save();                                           

}

.....

.....




I now realize, in STEP 2, once a record is inserted, it’s id (let’s say $modelMsgTo->id) is generated; say 6.

In STEP 3 during save function is called the model will have same id, since to insert new record the primary id should be different.

Due to this I was having problem. Am I right??

And thanks you once again mdomba for your cooperation. Have a good day.

I presume you have an autoincrement field as a primary key…

On the first insert (STEP2) the primary key is not assigned… so when an insert is made the database automatically inserts the next value… and Yii automatically fills the PK attribute to that value…

So for the STEP 3 you can just assign null to that field like $model->id=null… so that the database takes care of the autoincrement…

or even better

instead of $model->isNewRecord=true + taking care of the primary key field…

you just create "again" a new model like


$model=new MessageTo

, this way the primary key problem will be solved too

Ya I have added $model->isNewRecord=true but it didn’t. Your next idea of assigning $model->id=null, that would work fine but I have not tested yet.