Yii Framework Forum: Passing a variable to view - Yii Framework Forum

Jump to content

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

Passing a variable to view Rate Topic: -----

#1 User is offline   Big O 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 07-October 10

Posted 31 October 2010 - 11:44 PM

Hi,

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
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.

Thanks
Big O
0

#2 User is offline   zaccaria 

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

Posted 01 November 2010 - 02:14 AM

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 foo.com/B/create&a_id=21, and for each row in the table you can add a link "edit record".
0

#3 User is offline   Big O 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 07-October 10

Posted 01 November 2010 - 03:07 PM

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
0

#4 User is offline   Antonio Ramirez 

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

Posted 01 November 2010 - 05:35 PM

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:

B->render('viewofB',array('idofA'=>$ID_OF_A));

Then you can access the variable in the view as $idofA.
¿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

#5 User is offline   Big O 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 07-October 10

Posted 01 November 2010 - 06:51 PM

No, not i B, i am storing thsi relation in another table
I think calling
B->render('viewofB',array('idofA'=>$ID_OF_A));
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.
isn't..?

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




0

#6 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 01 November 2010 - 06:56 PM

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. :)
"Less noise - more signal"
0

#7 User is offline   Big O 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 07-October 10

Posted 02 November 2010 - 02:14 AM

Thanks buddy...

It made things simple

-Big O
0

#8 User is offline   Antonio Ramirez 

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

Posted 02 November 2010 - 08:57 AM

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

was actually the same I said:

B->render('viewofB',array('idofA'=>$ID_OF_A));

but with function redirect:

$this->redirect(array('viewofB','id'=>$model->b_id, 'idofA'=>$ID_OF_A));
¿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

#9 User is offline   Big O 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 07-October 10

Posted 02 November 2010 - 02:24 PM

Thanks Antonio :)

-Big O
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