viter
(Chekhiv)
February 5, 2010, 11:42am
1
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.
andy_s
(Arekandrei)
February 5, 2010, 11:55am
2
There can be two reasons:
Validation failed (use $misto->errors to get validation errors).
You rewrote beforeSave() or beforeValidate() method and forgot to put "return true;" in the end of a method.
thyseus
(Thyseus)
February 5, 2010, 12:14pm
4
viter:
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?
viter
(Chekhiv)
February 5, 2010, 12:20pm
5
save() works but the code that goes after save() is not executed, looks like the programm terminates after the data is saved.
cyberpol
(Cyberpol 777)
February 5, 2010, 12:26pm
6
Did you have behaviors attached?
cyberpol
(Cyberpol 777)
February 5, 2010, 12:28pm
8
Is the else executed in those cases?
viter
(Chekhiv)
February 5, 2010, 12:34pm
9
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')"));
?>
cyberpol
(Cyberpol 777)
February 5, 2010, 12:47pm
10
Please do this:
[code]
if ($misto->validate())
{
$ok = $misto->save();
echo "The result is ".$ok;
} else { …}
And tell us what is the result…
viter
(Chekhiv)
February 5, 2010, 1:05pm
11
The data is saved to database
and I get this text:
The result is 1
cyberpol
(Cyberpol 777)
February 5, 2010, 1:15pm
12
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…
viter
(Chekhiv)
February 5, 2010, 1:21pm
13
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] => )
cyberpol
(Cyberpol 777)
February 5, 2010, 1:32pm
14
Sorry, what i wanto to ask is if the $mid have some records or none…
Try whit this:
$mid = Mista::model()->findAll($mc);
viter
(Chekhiv)
February 5, 2010, 1:39pm
15
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().
cyberpol
(Cyberpol 777)
February 5, 2010, 2:04pm
16
What happens when the redirect is executed?
An error, an http error, a warning???
Nothing?
cyberpol
(Cyberpol 777)
February 5, 2010, 2:08pm
18
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));
viter
(Chekhiv)
February 5, 2010, 2:18pm
19
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.
cyberpol
(Cyberpol 777)
February 5, 2010, 2:27pm
20
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…