afterFind called twice?

I have a model with some processes to be done in afterFind().

Somehow in the update page or the view page, the afterFind() is called twice. (I printed some string, and they came out twice).

I checked the actionUpdate/actionView and they are only called once.

Does anyone know how to trace this or why it might be happening?

Nothing really fatal happens but I don’t think it’s efficient for the processes to be done twice.

My actionUpdate($id) is basic from giix


public function actionUpdate($id) {

		$model = $this->loadModel($id, 'Doc');

              

		if (isset($_POST['Doc'])) {

                         

			$model->setAttributes($_POST['Doc']);


			if ($model->save()) {

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

			}

		}


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

				'model' => $model,

				));

               

	}

and my model afterFind() now just reads


 public function afterFind() {

         print "hello"; 


         return parent::afterFind();

    }

Deleting the parent::afterFind() seems to have no effect.

Thanks for the help guys.

When exactly does the duplicate echo happens?

For example… if you have enabled the ajax validation and skip from one input field to the other your actionUpdate would be called at that time and the needed record would be loaded that in turn would call the afterFind()

It happens before the form submitted. When populating the form.

Even in view it prints twice. Impossible to call that ajax validation there, right?

I’m not using the ajax validation now. The afterSave is called only once.

Would it be possible that afterFind is called in the view scripts ? or in the _form with some other command?

I just don’t know what to look for to find where it is being called. I have no CActiveDataProvider/loadModel called in either view.php or update.php

Can anyone help please?

Sounds fairly normal. :)

It would happen if you have one or more behaviors attached to the model.

I have no custom behavior that I attach. How do I check where it’s calling ? Thanks

XDebug and Webgrind?

Maybe it happens because the model is called twice due to it returning two records (or more) ?

Hi,

No it’s in View, View shouldn’t call records twice right?

I will try and see how to use XDebug then.

The update action loads it once (first beforeFind), and then the view action loads it again (second beforeFInd); you are redirecting to the view action of the current controller in the update action.

Unless I’ve misunderstood (very likely at this time of night) :)

Hi Yun, did you solve it already?

An easy way to see the call stack is to just throw an exception and look at the page generated by Yii.

To see the 2nd+ calls, you’ll need some dirty tricks, though.