Yii Framework Forum: relation - Yii Framework Forum

Jump to content

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

relation Rate Topic: -----

#1 User is offline   alper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 12-November 10

Posted 22 December 2010 - 03:18 AM

i think this table dizayn is wrong but I have to continue with this dizayn.
table :
Bank { id,name }
bankaccount {bankid,name}
bankdetail {bankid,otherstaff}

In Bank Model Relation:
'bankdetails'=>array(self::HAS_MANY, 'BANKDETAIL', '', 'on'=>'bankid=id')


In bankdetail Model Relation:
'banks' => array(self::BELONGS_TO, 'BANK', 'bankid'),


i take values :
$values = BANKDETAIL::model()->with('banks')->findAll(); 


---- this is working.
I need to add "bankaccount table" to read "bankaccount.name" in bankdetail model releation. I dont know how can i do this.
0

#2 User is offline   Antonio Ramirez 

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

Posted 22 December 2010 - 03:28 AM

View Postalper, on 22 December 2010 - 03:18 AM, said:

i think this table dizayn is wrong but I have to continue with this dizayn.
table :
Bank { id,name }
bankaccount {bankid,name}
bankdetail {bankid,otherstaff}

In Bank Model Relation:
'bankdetails'=>array(self::HAS_MANY, 'BANKDETAIL', '', 'on'=>'bankid=id')


In bankdetail Model Relation:
'banks' => array(self::BELONGS_TO, 'BANK', 'bankid'),


i take values :
$values = BANKDETAIL::model()->with('banks')->findAll(); 


---- this is working.
I need to add "bankaccount table" to read "bankaccount.name" in bankdetail model releation. I dont know how can i do this.


create a join table for both tables in case you dont put the id of the bankaccount in bankdetail and viceversa
bankaccount {id, otherstuff}
bankaccountdetail {bankaccountid, bankdetailid}

then you just need to create a MANY_MANY relationship type. Good enough reference post (http://www.yiiframew...MANY#entry47769)

but, if you include the ID on *any* of the tables, then is a HAS_MANY - BELONGS_TO
0

#3 User is offline   alper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 12-November 10

Posted 22 December 2010 - 03:37 AM

Hmm i see, thank you for response.
Unfortunatly i dont have permision to add or change table. are there any solution with this design ?
0

#4 User is offline   Antonio Ramirez 

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

Posted 22 December 2010 - 05:16 AM

how are the bankaccount that you are going to add and bankdetail related?
0

#5 User is offline   alper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 12-November 10

Posted 22 December 2010 - 06:32 AM

Bank :
id = 1 name = A bank
Bankaccount:
Bankid = 1 , Account Name = Account 1
Bankdetail :
bankid = 1 , Otherstaff = bla bla
Bankid = 1 , otherstaff = cla cla

sql output :
A Bank , Account 1, Bla bla
A Bank , Account 1, Cla cla

I want output like this. I know this is wrong or useless but i need this design..
I can write manuel sql to make this but is it possible with using relations ?

Bankdetail::model->with(array('banks relation',' bankaccount relation'))...

$model->bank relation->name
$model->otherstaf
$model->bankaccount relation->account name
0

#6 User is offline   FlyBot 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 20-January 09
  • Location:Ukraine

Posted 22 December 2010 - 07:48 AM

In Bank Model Relation:
'bankdetails'=>array(self::HAS_MANY, 'BANKDETAIL', '', 'on'=>'bankid=id')
'bankaccount'=>array(self::HAS_MANY, 'BANKACCOUNT', '', 'on'=>'bankid=id')


$values = Bank::model()->with('bankdetails', 'bankaccount')->findAll(); 


Guide to AR relations
1

#7 User is offline   Antonio Ramirez 

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

Posted 22 December 2010 - 05:37 PM

I would not express my self better.

Follow FlyBot help, he is right, that was a basic relationship setup..
0

#8 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,710
  • Joined: 10-October 10
  • Location:Denmark

Posted 22 December 2010 - 05:47 PM

Actually, it's even simpler:
'bankdetails' => array(self::HAS_MANY, 'BANKDETAIL', 'bankid'),
'bankaccounts' => array(self::HAS_MANY, 'BANKACCOUNT', 'bankid'),

Notice: no 'on'.
And then, if using lazy loading, you can just use it like this:
$values = Bank::model()->findAll();

Or eagerly (like Fly said):
$values = Bank::model()->with('bankdetails', 'bankaccount')->findAll();

In the view:
foreach($values->bankaccounts as $bankaccount) {
echo $bankaccount->balance;
}


It's one of those things which feels like cheating - or almost too simple - but that's Yii.. :)
"Less noise - more signal"
2

#9 User is offline   alper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 12-November 10

Posted 23 December 2010 - 07:26 AM

thank you!!
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