Yii Framework Forum: Passing Primary Key value to Foreign Key table on same form view - Yii Framework Forum

Jump to content

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

Passing Primary Key value to Foreign Key table on same form view Rate Topic: -----

#1 User is offline   Yasir Liaqat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 14-June 11

Posted 22 April 2012 - 09:51 AM

Hi Guys,

I am facing a problem passing value of primary key of parent table to the child table where it is foreign key. They are both on the same form and validated. But I am not able to find out how to solve this problem.

Any hints?

Thanking you in advance.

Quote

Table A
Table B

Table A and Table B are having one combined form. Table A has primary key UniqueID. Table B has TableA_UniqueID referencing to Table A. If I need to get data on one form, then how do I resolve providing Table A UniqueID value to Table B TableA_UniqueID.

Yii - Yes it is
Simply a best php framework on earth

Cheap Business Cards Printing | Cheap Flyers Printing | Cheap Posters Printing | Cheap Website Design
0

#2 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 22 April 2012 - 10:06 AM

View PostYasir Liaqat, on 22 April 2012 - 09:51 AM, said:

Hi Guys,

I am facing a problem passing value of primary key of parent table to the child table where it is foreign key. They are both on the same form and validated. But I am not able to find out how to solve this problem.

Any hints?

Thanking you in advance.


this works? update/create action in your control

if ($modelA->save()){
   $modelB->TableA_UniqueID = $modelA-> UniqueID;
   $modelB->save();
}



I enjoy the Yii.sy coding life here.
0

#3 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,168
  • Joined: 05-January 12
  • Location:Morocco

Posted 22 April 2012 - 11:10 AM

I imagine you're stuck only with new records. In That case, don't forget to refresh your model after saving so That the id field / property is correctly populated from the db, either it is an autoincrement pk or some other guid.

if($modelA->save()) {
  ...
  $modelA->refresh();
  $modelB->aId = $modelA->id;
  if($modelB->save()) {
    ...
  }
  ...
}


If you're updating a record from Model A, I guess it already works smoothly.
0

#4 User is offline   Yasir Liaqat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 14-June 11

Posted 22 April 2012 - 11:56 AM

@rootbear @bennouna

Thanks to both for your response to my post. Both of the above are really helpful. Based on the information provided by you both, I believe the problem is definately solved because I can clearly see it is making an absolute sense.

I will update you anyway.

Many thanks once again.
Yii - Yes it is
Simply a best php framework on earth

Cheap Business Cards Printing | Cheap Flyers Printing | Cheap Posters Printing | Cheap Website Design
0

#5 User is offline   shahkhairudin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 09-May 13

Posted 14 May 2013 - 09:14 PM

Hi Yasir,

how was it? which one is working for u?

thanks.
There are crimes of passion and crimes of logic. The boundary between them is not clearly defined. Im building something interesting with Yii Framework and i love it. @shahkhairudin
0

#6 User is offline   shahkhairudin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 09-May 13

Posted 14 May 2013 - 09:17 PM

View Postrootbear, on 22 April 2012 - 10:06 AM, said:

this works? update/create action in your control

if ($modelA->save()){
   $modelB->TableA_UniqueID = $modelA-> UniqueID;
   $modelB->save();
}





thanks rootbear. solved my problem. thanks again! ;)
There are crimes of passion and crimes of logic. The boundary between them is not clearly defined. Im building something interesting with Yii Framework and i love it. @shahkhairudin
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