beforeSave() not called correctly

Hi all,

I have searched both forum and API trying to figure out why beforeSave() does not get called correctly.

I am trying to call beforeSave() on a user model with the following function:

protected function beforeSave() {


	$test = '-----------------> a beforeSave';


	echo Yii::trace(CVarDumper::dumpAsString($test),'test');


	return parent::beforeSave();


}

Which si never displayed in the log. However, when I change the return value to false, the message will show up in the log (WebLog).

The saving of User models goes fine (when i use the parent::beforeSave()), and they get persisted, so it can not be a validation error.

Some info: Yii 1.1.7, running on Ubuntu 2.6.38-8-generic.

Yii is totally new to me, so this may be very simple.

Any help is much appreciated!

Cheers

Where do you have Yii configured to place trace messages? When you change the return value to false, you are creating an error condition, which is most likely why you are able to see it in the log at that point.

You need to explicitly turn on showing traces in your configuration file, something like:




                'log'=>array(

                        'class'=>'CLogRouter',

                        'routes'=>array(

                                array(

                                          'class'=>'CFileLogRoute',

                                          'levels'=>'error, warning',

                                          'logFile' => 'error.log',

                                          ),

                                array(

                                          'class' => 'CFileLogRoute',

                                          'levels' => 'info',

                                          'logFile' => 'application.log',

                                          ),

                                array(

                                          'class' => 'CFileLogRoute',

                                          'levels' => 'trace',

                                          'logFile' => 'debug.log',

                                          ),

)),



and then in your index.php:




// remove the following lines when in production mode

defined('YII_DEBUG') or define('YII_DEBUG',true);

// specify how many levels of call stack should be shown in each log message

defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);



Hi Preston,

and thank you for an VERY quick answer :D

This my logging configuration. And for now i want to get the tracing in the web page.

‘log’=>array(

		'class'=>'CLogRouter',


		'routes'=>array(


    		array(


				'class'=>'CFileLogRoute',


				'levels'=>'error, warning, trace',


                'logFile'=>'application.log',


			),


			// uncomment the following to show log messages on web pages


			array(


				'class'=>'CWebLogRoute',


				'enabled'=>true,


				'levels'=>'trace, info',


			),


		),


	),

And in the main:

<?php

// change the following paths if necessary

$yii=dirname(FILE).’/framework/yii.php’;

$config=dirname(FILE).’/protected/config/main.php’;

// remove the following lines when in production mode

defined(‘YII_DEBUG’) or define(‘YII_DEBUG’,true);

// specify how many levels of call stack should be shown in each log message

defined(‘YII_TRACE_LEVEL’) or define(‘YII_TRACE_LEVEL’,5);

require_once($yii);

Yii::createWebApplication($config)->run();

With this configuration, I do still not get the trace in the Application.log, which to my understanding is the one showing in the web page.

And as you are pointing to, this is a logging issue, and not the beforeSave not being called as i can modify the model here.

Any suggestion to get the logger to work if it is not an error? I do specify the level as trace.

Cheers!

So putting


Yii::trace("something");

gives you no output in application.log? That is really weird. Do either:


Yii::log("something", 'trace');

or


Yii::log("something", 'error');

Work?

Do you have


'preload' => array('log'),

in your main.php config file?

Yes, I have the option to preload log in my configuration file. And no success with the three different level options.

I do think this is very weird. The beforeSave gets called, so it is in the codepath. Another thing that is really weird is that I use the same way for triggering logging in other parts of the code.

For example in the model class when fetching:

   public static function model(&#036;className=__CLASS__)


{


	&#036;test = '-----------------&#62; a model in user';


	echo Yii::trace(CVarDumper::dumpAsString(&#036;test),'vardump');


	return parent::model(&#036;className);


}

Will do the logging.

Is there anything else special with beforeSave, except it "overrides" the parents method?

Thank you for your thoughts on this matter.

In the parent chain the corresponding event will be raised, e.g used by handlers in behaviors. What happens if you don’t call the parent and just return true?

/Tommy

Trying to just return true gives the same result. Everything works (i.e saving) but no logging. However, if i return false the logging occurs.

I know I had the logging working on success scenarios earlier. I have gone through the code to roll back to those settings, but no luck. I have not made any changes to any other configuration either.

Thanks for your replies! I cannot debug Yii much at this stage unfortunately.

What does your (controller) code do in the case when save() returns true.

/Tommy

The use case is when a new user registers, and this is the code in the controller




public function actionRegister()

	{	

		$test = '-----------------> a register';

		echo Yii::trace(CVarDumper::dumpAsString($test),'vardump');

		

		$model=new User;

		

		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			if($model->save())

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

		}

		echo Yii::trace(CVarDumper::dumpAsString('in register2'),'vardump');

		

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

			'model'=>$model,

		));

	}



So it is just a redirect that occurs. And I do get redirected…

I noticed you used echo Yii::trace(…) instead of just Yii::trace(…). You’ve better remove the echo, but I doubt it’s the cause of your problem.

I have no more clues at the moment.

/Tommy

Hi,

that did not solve it unfortunately. Thanks for your time and effort. Let me know if you get any more clues.

Cheers!