nemo
(Nemoneel)
1
Hey Guys, I have a wearied problem. I have two function in my model:
public function insertA()
{
…
}
public function insertB()
{
…
}
when I call those function from controller like this
$form->insertA();
$form->insertB();
only first function insert data in my database. Can anyone tell me where is that problem to call those function
THanks
zaccaria
(Matteo Falsitta)
2
This code has not evident mistakes, maybe the problem lies in the code of the functions.
nemo
(Nemoneel)
3
Here is my two function
public function insertA()
{
$this->a_id = 4;
$this->adress = $inserta1 .$t. $inserta2 .$t. $inserta3 .$t. $inserta4;
$this->save();
}
public function insertB()
{
$this->a_id = 5;
$this->adress = $insertb1 .$t. $insertb2 .$t. $insertb3 .$t. $insertb4;
$this->save();
}
in controller
public function actionInsert()
{
$form=new Test;
if(isset($_POST['Test']))
{
$form->attributes=$_POST['Test'];
if($form->validate())
if (($form->a ) && ($form->b )) {
$system ='a,b';
}
if ($system == 'a,b') {
$form->insertA();
$form->insertB();
$this->redirect(array('test','taskidentifier'=>$form->taskidentifier));
}
}
}
$this->render('insert',array('form'=>$form));
}
zaccaria
(Matteo Falsitta)
4
If you want to insert 2 record, you have to create 2 activerecords.
Something like that in the controller:
$model=new ClassName;
$model->a_id = 4;
$model->adress = [...];
$model->save();
$model=new ClassName;
$model->a_id = 5;
$model->adress = [...];
$model->save();
DavidHHuan
(Davidhhuan)
5
you can check your log that whether the sql in insertB() runs or not.
you can test in insertB() with Yii::app()->end() to see that it runs or not.
also, in your controller, after $form->insertB(), you can use this to test it.
echo CHtml::errorSummary($form);
die();
Zaccaria is right… if you wish to insert two models you need to create them… by using $this keyword you know where are you refering to right?