ActiveRecord validation fail causes subsequent db fails

Not sure if this is intended behaviour, Here goes -

I have a db with a few tables, one is a ‘logger’ table, the other are simple tables with some fields that are required.

If I have save a record (using AR) using something like this -




	if(!$vehicle_engine->save())

	{

   		$this->DbLogIt(SELF::PROCESS_STYLES, SELF::ED_ERROR_SAVE_FAILED, 'Vehicle Engine Style Save FAILED. StyleEId :' . $style_ed_id . ' Make : ' . $style_make . ' Model : ' . $style_model . ', ERROR : ' . print_r($vehicle_engine->errors, true), $styles_url);

        	return false;

	}



If the save() fails due to a field violation (ie, missing field or field length too long, etc) it calls this simple function that logs the error to the db. However when it tries to save the record it also now fails even though it shouldn’t.

Here’s the code to the logger in case I missed something (like clearing an error state??)




    public function DbLogIt($name, $status, $msg = '', $url = SELF::MSG_URL_NOT_YET_SET)

	{

		$log = new Logger();

		$log->isNewRecord = true;

		$log->data_name = $name;

		$log->ed_status = $status;

		$log->ed_msg = $msg;

		$log->ed_url = $url;

		

		if(!$log->save())

		{

			// last ditch attempt to see the error 

			

			echo '<br>LOG RECORD NOT SAVED<BR>';

			echo 'Name 	:' . $name . '<br>';

			echo 'Status   :' . $status . '<br>';

			echo 'Message  :' . $msg . '<br>';

			echo 'URL  	:' . $url . '<br>';

		}

	}



The question is after a rule violation in using AR should it affect other AR table? Or am I just again doing something out of whack for Yii2

Thanks for any help, and yes I just spotted the non-needed ‘isNewRecord’ ;)

Found the issue after actually looking at the logs->error member. It seems I had more then 255 characters in the message that was causing a cascade of fails. Key learn from this is LOOK AT THE ERROR variable it will tell a lot about the problem.

Sandy