Passing a variable to view
Posted 31 October 2010 - 11:44 PM
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. foo.com/B/create&a_id=21
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.
Posted 01 November 2010 - 02:14 AM
Here you can add a button "add more b", that will lead the user to foo.com/B/create&a_id=21, and for each row in the table you can add a link "edit record".
Posted 01 November 2010 - 05:35 PM
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.
Posted 01 November 2010 - 06:51 PM
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
$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.
Posted 02 November 2010 - 08:57 AM
was actually the same I said:
but with function redirect: