How to get id after new record is created

Hi all,

I have the following situation:

Table_A:

-idA

-a1

-a2

-processID (FK to a process table with only 2 entries (1 and 2)

Table_B:

-idB

-idA (FK)

-b1

-b2

Table_C

-idC

-idA (FK)

-c1

-c2

When I create a new record of A in actionCreate, depending on the processID (selected by user during create), if user select process1 then a record of B should also be created, and if process2 is selected then a record of C should be created…

What I nead is:

1.How to figure out which processID was selected by user, to decide which also to create (B or C)?

2.When I create the new model of B or C (depending on process selection) how do I get the idA of the new created record to insert it as a FK in the B or C that should also be created?

So I want to know how to do the following:




public function actionCreate()

{

   $model=new A;

   ...

   $model->save();


   if(A.processID == 1)

      $B=new B; (here I also want to know about question 2)

      $B->save();

   else

      $C=new C;

      $C->save();

}



You can override afterSave() method of model Table_A, so:




// We are in Table_A model

// We have idA valorized because this code is executed after save.

public function afterSave()

{

     parent::afterSave();


     if ($this->processID == 1)

     {

          $b=new B; 

          $b->idA = $this->id;

          $b->save();

     }

     else if($this->processID == 2)

     {

          $c=new C;

          $c->idA = $this->id;

          $c->save();

     }

}



Thank you for your answer.

It works except of a small problem…

It is creating all the time a record in B, no matter what process I select…

Have you used my code?

Because in your code you have missed curly brackets.

:) The problem was that I had set processID as default 1 :lol:

Now it is working

Thank you Fabrizio :)