save()

Hello everyone.

I have a piece of code:


if($misto->save())

{

        $m = $misto->misto;

	$o = $misto->oblid;

	$mc = new CDbCriteria;

	$mc->condition = "misto LIKE :sterm and oblid like :oblid";

	$mc->params = array(":sterm"=>"$m",":oblid"=>"$o");

	$mid = Mista::model()->find($mc);		

	$this->redirect(array('site/town','mid'=>$mid,'oblid'=>$misto->oblid));

}

else

{

	echo "record is not saved";

}

My problem is: the code that follows


if($misto->save())

is not run.

Can anyone tell me why?

Thank you.

There can be two reasons:

  1. Validation failed (use $misto->errors to get validation errors).

  2. You rewrote beforeSave() or beforeValidate() method and forgot to put "return true;" in the end of a method.

  1. No. Validation is not failed because I can see data inserted in my database.

  2. I didn’t rewrite any of these methods.

When the data get’s inserted in your database, then save() does work?

save() works but the code that goes after save() is not executed, looks like the programm terminates after the data is saved.

Did you have behaviors attached?

No

Is the else executed in those cases?

No, else is not executed either.

the code that I showed is a part of my controller, the whole action is this:


public function actionTownadd()

	{

		$misto = new Mista;

		$misto->misto=$_POST['misto'];

		$misto->oblid=$_POST['oblid'];

		if ($misto->validate())

		{

			

			if($misto->save())

			{

				$m = $misto->misto;

				$o = $misto->oblid;

				$mc = new CDbCriteria;

				$mc->condition = "misto LIKE :sterm and oblid like :oblid";

				$mc->params = array(":sterm"=>"$m",":oblid"=>"$o");

				$mid = Mista::model()->find($mc);

				$this->redirect(array('site/town','mid'=>$mid,'oblid'=>$misto->oblid));

			}

			else

			{

				echo "record is not saved";

			}

		}

		else

		{

			$message = array();

			$message = "Error!";

			echo $message;

		}

	}

And it’s called from my view with ajax:


<?php echo CHtml::ajaxSubmitButton ("add",

                              CController::createUrl('site/townadd'), 

                              array('update' => '#message'), array('class'=>'knopka', 'id'=>"dodaty", 'value'=>"додати", 'onmouseover'=>"knopka_style(1,'s')", 'onmouseout'=>"knopka_style(0,'s')"));

?>

Please do this:

[code]

if ($misto->validate())

{

$ok = $misto->save();

echo "The result is ".$ok;

} else { …}

And tell us what is the result…

The data is saved to database

and I get this text:

The result is 1

Then the problem is in one of these lines:




$m = $misto->misto;

$o = $misto->oblid;

$mc = new CDbCriteria;

$mc->condition = "misto LIKE :sterm and oblid like :oblid";

$mc->params = array(":sterm"=>"$m",":oblid"=>"$o");

$mid = Mista::model()->find($mc);

$this->redirect(array('site/town','mid'=>$mid,'oblid'=>$misto->oblid));



Comment the redirect and do a print_r($mid) and tell us what do you see…

I get this:


Mista Object ( [_md:private] => CActiveRecordMetaData Object ( [tableSchema] => CMysqlTableSchema Object ( [schemaName] => [name] => mista [rawName] => `mista` [primaryKey] => mid [sequenceName] => [foreignKeys] => Array ( ) [columns] => Array ( [mid] => CMysqlColumnSchema Object ( [name] => mid [rawName] => `mid` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => 1 [isForeignKey] => [_e:private] => [_m:private] => ) [oblid] => CMysqlColumnSchema Object ( [name] => oblid [rawName] => `oblid` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => [isForeignKey] => [_e:private] => [_m:private] => ) [misto] => CMysqlColumnSchema Object ( [name] => misto [rawName] => `misto` [allowNull] => [dbType] => varchar(255) [type] => string [defaultValue] => [size] => 255 [precision] => 255 [scale] => [isPrimaryKey] => [isForeignKey] => [_e:private] => [_m:private] => ) ) [_e:private] => [_m:private] => ) [columns] => Array ( [mid] => CMysqlColumnSchema Object ( [name] => mid [rawName] => `mid` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => 1 [isForeignKey] => [_e:private] => [_m:private] => ) [oblid] => CMysqlColumnSchema Object ( [name] => oblid [rawName] => `oblid` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => [isForeignKey] => [_e:private] => [_m:private] => ) [misto] => CMysqlColumnSchema Object ( [name] => misto [rawName] => `misto` [allowNull] => [dbType] => varchar(255) [type] => string [defaultValue] => [size] => 255 [precision] => 255 [scale] => [isPrimaryKey] => [isForeignKey] => [_e:private] => [_m:private] => ) ) [relations] => Array ( [misto] => CBelongsToRelation Object ( [joinType] => LEFT OUTER JOIN [on] => [alias] => [with] => Array ( ) [name] => misto [className] => Oblasti [foreignKey] => oblid [select] => * [condition] => [params] => Array ( ) [group] => [having] => [order] => [_e:private] => [_m:private] => ) ) [attributeDefaults] => Array ( ) [_model:private] => Mista Object ( [_md:private] => CActiveRecordMetaData Object *RECURSION* [_new:private] => [_attributes:private] => Array ( ) [_related:private] => Array ( ) [_c:private] => [_pk:private] => [_errors:private] => Array ( ) [_validators:private] => [_scenario:private] => [_e:private] => [_m:private] => ) ) [_new:private] => [_attributes:private] => Array ( [mid] => 32 [oblid] => 2 [misto] => v ) [_related:private] => Array ( ) [_c:private] => [_pk:private] => 32 [_errors:private] => Array ( ) [_validators:private] => [_scenario:private] => update [_e:private] => [_m:private] => ) 

Sorry, what i wanto to ask is if the $mid have some records or none…

Try whit this:




$mid = Mista::model()->findAll($mc);



I figured out that in


$this->redirect(array('site/town','mid'=>$mid,'oblid'=>$misto->oblid));

instead of ‘mid’=>$mid I have to use ‘mid’=>$mid->mid.

‘mid’=>$mid->mid returns correct data from my database.

But redirection still does not work.

May be I use wrong parameters in in my $this->redirect().

What happens when the redirect is executed?

An error, an http error, a warning???

Nothing?

Nothing.

weird!!!

do you try to redirect to some other controller that doesn’t need get parameters?

or what about




$this->render('site/town',array('mid'=>$mid->mid,'oblid'=>$misto->oblid));



I try to redirect to the same controller to this action


public function actionTown()

	{

		$param = array();

		$param['mid'] = $_GET['mid'];

		$param['oblid'] = $_GET['oblid'];

		$this->render('misto',$param);

	}

$this->render() will not do because as the controller is colled via ajax the rendered view will return back to my current view but I need redirection.

But if it is an ajax call then the redirect redirects (sorry for the redundancy) that ajax call!!!

You need another approach to redirect the page that fires the ajax call (call it pageA), like return some value from the ajax call and then use javascript in pageA to do the redirect…