Ajax Function Writing Twice To My Table

I am new to Yii and Ajax and facing this problem:

My application displays entreprise information with the following text to hide the real phone number: "*** Primary Phone ". When the user clicks on " Primary Phone ***" I use the CHtml::ajaxLink to call an ajax function that writes in a log table and displays the primary phone # on the screen.

Code used in my Form hiding the primary phone #:

<div id=“pp_<?php echo $data[‘entreprise_id’]; ?>”>

<?php echo CHtml::ajaxLink(‘Primary Phone’,

 Yii::app()-&gt;createUrl( 'logClicked/ajaxLog'), 


 array( 

‘update’ => ‘#pp_’.$data[‘entreprise_id’],

‘type’ => ‘POST’,

‘data’=> array( ‘ent’=>$data[‘entreprise_id’], ‘type’=>LogClicked::PHONE_CLICKED,

) ));?>

</div>

Code used in my Controller

public function actionAjaxLog()


{


	// log the click


	&#036;lc = new LogClicked();


	&#036;lc-&gt;entreprise_id = &#036;_POST['ent'];


	&#036;lc-&gt;clicked_type = &#036;_POST['type'];


	&#036;lc-&gt;userid = Yii::app()-&gt;user-&gt;isGuest ? LogClicked::SYSTEM_USERID : Yii::app()-&gt;user-&gt;id;


	&#036;lc-&gt;timestamp = new CDbExpression('NOW()');


	&#036;lc-&gt;save(false);	


	


	// action


	switch (&#036;_POST['type']) {


		


		case LogClicked::PHONE_CLICKED:


			Yii::app()-&gt;getModule('Entreprise');				


			echo Entreprise::model()-&gt;getPhone(&#036;_POST['ent']);


			break;


		


	}


	


	Yii::app()-&gt;end();


}

With the testing I have done and using the debugger I believe the code is being executed twice but I don’t know how to prevent this.

Any help would be appreciated.

Thanks

Firstly, i try to comment $ic->save(false); to check that no row will be write to the db table

from other code.

Then i’ll check this:




case LogClicked::PHONE_CLICKED:

Yii::app()->getModule('Entreprise');	

echo Entreprise::model()->getPhone($_POST['ent']);

break;



What Enterprise::model()->getPhone() method contains?

Thanks for your reply…

The application is behaving as expected when commenting the $lc->save(false) line…nothing gets written to the table.

Then, I uncommented the save method and commented the code in the case statement. The phone number didn’t get displayed on the screen and the table got written twice.

The Enterprise::model()->getPhone() only reads the phone number from my Entreprise table:




public function getPhone($ent) {	

$rec = Entreprise::model()->findByPk($ent);	

return $rec['phone_primary'];

}



Since I am still trying to get my head around calling an Ajax function I kept my code very simple. I could be wrong but I don’t think the problem is directly related my code. It’s probably related to the environment like a flag that I didn’t set properly or something missing either in my controller, model or in my ajax function. I hope this make sens!!

First a suggest for this code:




public function getPhone($ent) {        

$rec = Entreprise::model()->findByPk($ent);     

return $rec['phone_primary'];

}



I’ll make a static method, so:




Entreprise::getPhone($_POST['ent']);


public static function getPhone($ent) {        

$rec = Entreprise::model()->findByPk($ent);     

return ($rec!=null)?$rec->phone_primary:null;

}



Then with firebug i’ll check that your javascript code not call twice remote url via ajax.

The code change didn’t do much but the use of firebug was a great suggestion and confirmed that my javascript code is being called twice.

I did more testing and created a new Yii project, copied the code relevant to my Ajax testing and confirmed that my code is working well.

Thanks for your help Fabrizio