Passing a variable to view


I have 2 indirectly related models A and B

After creating a recored of model A, in view of A i show an option to create recored of model B. e.g. create B.

When user clicks on this option I pass A’s id by calling B’s create action using this url i.e.

after this.

While creating a record for model B I use this id to store the relation with A.

Now i have already created B’s record correctly and can see it in View.

In B’s view I want to allow user to create more of B’s records but here i don’t have access to A’s id “a_id”

coz i am already in B’s controller.

Could you pls tell me how to get this id from A and pass it to the current controller B.

Waiting for reply.


Big O

In the show of a model il A you can add a table for showing the related record in B.

Here you can add a button "add more b", that will lead the user to, and for each row in the table you can add a link "edit record".

thanks for reply…

i explained that i am already able to add this link in A’s view.

but in B’s view i want to add this link to add more B’s recored with A’s id.

i dont get A’s id there

-Big O

This is a bit confusing: While creating a record for model B I use this id to store the relation with A

Where do you store that relation? in B? Because if it is in B just get the id of A from its relation… otherwise, if afterwards you go to B’s view and after receiving the id of A, and before you render it do:


Then you can access the variable in the view as $idofA.

No, not i B, i am storing thsi relation in another table

I think calling


will not allow user to view the B’s record coz it A’s id, and i want to shwo user B’s record which has just been created.


what i am doing is

From A’s controller I let user do “create B”

When user clicks on it, i pass a_id along with create/B&a_id=XXNo

This allows me to enter into B’s create page.

In B i have a pre filter which allows me to load the right record of A by using XXNo just before creating B.

I create a relation Rel_A_B record in B->Create() and there, fetch A’s id (i already have correct A record through pre filter)

and B’s id and store these in this Rel_A_B record.

After calling save() on B’s Model and Rel_A_B

i am redirecting to B’s view

like this

$this->redirect(array(‘view’,‘id’=>$model->b_id)); // b_id is needed to show user B’s record just created

So it takes me to B’s view, i want to do it coz i want to show to the user what it has created.

Now on B’s view i want to show “create More of B” link, but i don’t know how to show the a_id here coz

By this time i have lost A’s record.

Let me know if there is still some confusion.

-Big O

Pass the id of A when you redirect.

Append ‘a_id’ => a->id to the URL.

Then you can use $_GET[‘a_id’] to get it. :)

Thanks buddy…

It made things simple

-Big O

Looking at your last explanation jacmos has told you the right answer…

was actually the same I said:


but with function redirect:

$this->redirect(array(‘viewofB’,‘id’=>$model->b_id, ‘idofA’=>$ID_OF_A));

Thanks Antonio :)

-Big O