Yii Framework Forum: Can not call function from controller - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Can not call function from controller Rate Topic: -----

#1 User is offline   nemo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 24-February 10

Posted 29 October 2010 - 07:48 AM

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
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 29 October 2010 - 08:01 AM

This code has not evident mistakes, maybe the problem lies in the code of the functions.
0

#3 User is offline   nemo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 24-February 10

Posted 29 October 2010 - 08:23 AM

View Postzaccaria, on 29 October 2010 - 08:01 AM, said:

This code has not evident mistakes, maybe the problem lies in the code of the functions.



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

#4 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 29 October 2010 - 08:28 AM

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



0

#5 User is offline   Davidhhuan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 270
  • Joined: 08-September 09

Posted 29 October 2010 - 08:30 AM

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

no pain, no gain...

My Blog : http://cnblogs.com/davidhhuan
My website: http://sharefamily.net/
0

#6 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 29 October 2010 - 09:36 AM

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?
┬┐How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

Share this topic:


Page 1 of 1
  • 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