relation

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.yiiframework.com/forum/index.php?/topic/9694-many-many-question/page__p__47769__hl__MANYMANY#entry47769)

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

Hmm i see, thank you for response.

Unfortunatly i dont have permision to add or change table. are there any solution with this design ?

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

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

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

I would not express my self better.

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

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… :)

thank you!!