Yii Framework Forum: save() - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

save() Rate Topic: -----

#1 User is offline   viter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 177
  • Joined: 21-January 10
  • Location:Ukraine Ivano-Frankivsk

Posted 05 February 2010 - 06:42 AM

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.
0

#2 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 05 February 2010 - 06:55 AM

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.
0

#3 User is offline   viter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 177
  • Joined: 21-January 10
  • Location:Ukraine Ivano-Frankivsk

Posted 05 February 2010 - 07:04 AM

Quote

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.
0

#4 User is offline   thyseus 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 300
  • Joined: 18-April 09
  • Location:Leipzig, Germany

Posted 05 February 2010 - 07:14 AM

View Postviter, on 05 February 2010 - 07:04 AM, said:

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


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

#5 User is offline   viter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 177
  • Joined: 21-January 10
  • Location:Ukraine Ivano-Frankivsk

Posted 05 February 2010 - 07:20 AM

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

#6 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 05 February 2010 - 07:26 AM

Did you have behaviors attached?
Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#7 User is offline   viter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 177
  • Joined: 21-January 10
  • Location:Ukraine Ivano-Frankivsk

Posted 05 February 2010 - 07:27 AM

No
0

#8 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 05 February 2010 - 07:28 AM

Is the else executed in those cases?
Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#9 User is offline   viter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 177
  • Joined: 21-January 10
  • Location:Ukraine Ivano-Frankivsk

Posted 05 February 2010 - 07:34 AM

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')"));
?>

0

#10 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 05 February 2010 - 07:47 AM

Please do this:

[code]
if ($misto->validate())
{
$ok = $misto->save();
echo "The result is ".$ok;
} else { ....}

And tell us what is the result...
Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#11 User is offline   viter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 177
  • Joined: 21-January 10
  • Location:Ukraine Ivano-Frankivsk

Posted 05 February 2010 - 08:05 AM

Quote

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
0

#12 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 05 February 2010 - 08:15 AM

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...

Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#13 User is offline   viter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 177
  • Joined: 21-January 10
  • Location:Ukraine Ivano-Frankivsk

Posted 05 February 2010 - 08:21 AM

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] => ) 

0

#14 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 05 February 2010 - 08:32 AM

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

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


Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#15 User is offline   viter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 177
  • Joined: 21-January 10
  • Location:Ukraine Ivano-Frankivsk

Posted 05 February 2010 - 08:39 AM

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().
0

#16 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 05 February 2010 - 09:04 AM

What happens when the redirect is executed?
An error, an http error, a warning???
Nothing?
Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#17 User is offline   viter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 177
  • Joined: 21-January 10
  • Location:Ukraine Ivano-Frankivsk

Posted 05 February 2010 - 09:05 AM

Nothing.
0

#18 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 05 February 2010 - 09:08 AM

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));

Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#19 User is offline   viter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 177
  • Joined: 21-January 10
  • Location:Ukraine Ivano-Frankivsk

Posted 05 February 2010 - 09:18 AM

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.
0

#20 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 05 February 2010 - 09:27 AM

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...
Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users