5

I'm seeing all sorts of different ways to handle many to many relationships in Yii. However, the examples I'm seeing aren't fully fleshed out and I feel like I'm missing something.

For instance, Larry Ullman's tutorial doesn't use the self::MANY_MANY relation - http://www.larryullman.com/2010/08/10/handling-related-models-in-yii-forms/

So as far as adding and retrieving records, whats the standard way for handling Many to Many in the Model, Controller and View?

clarification: I suppose Im looking for an example involving 2 tables, related by many-to-many, where I can see not only both models, but controllers and views as well so I can fully understand whats going on.

Kai
  • 38,985
  • 14
  • 88
  • 103
djt
  • 7,297
  • 11
  • 55
  • 102

2 Answers2

5

You actually need the 3 tables (so an extra model for the pivot table), but once you have it you can actually use the regular yii relation functionality.

For example a project of mine has a many-to-many relation between a Purchase and a Transaction (please don't ask why :) ). So there is a Purchase model, a Transaction model and a PurchaseToTransaction model that establishes links between them. I can just do a $oPurchase->transactions and Yii will handle the many-to-many part using the relation, it is defined as follows:

'transactions' => array(self::MANY_MANY, 'Transaction', 'PurchaseToTransaction(purchaseId, transactionId)')

Note that for the Transactions, the same applies but the other way around:

'purchases'   => array(self::MANY_MANY, 'Purchase', 'PurchaseToTransaction(transactionId, purchaseId)'),

So both $oPurchase->transactions and $oTransaction->purchases are automatically handled by Yii.

In conclusion it can indeed handle Many-to-Many using relations (at least the reading part, for writing you still need arbitrary solutions).

Blizz
  • 8,082
  • 2
  • 33
  • 53
  • and what would these arbitrary solutions be? Also, do I have to create a model for the bridge table? – djt Mar 14 '11 at 17:51
  • yes you have to create the model, but it can just be an empty model with the name of the pivot table, no further logic. What the other solutions is concerned, there is this behavior for example: http://www.yiiframework.com/extension/save-relations-ar-behavior/ also the last Yii versions will include enhanced functionality: http://code.google.com/p/yii/issues/detail?id=1117 – Blizz Mar 14 '11 at 19:56
  • 1
    You don't need to create a model class for the intermediate table unless there is extra logic you want to attach there. – Matt Kantor May 06 '11 at 19:27
  • @Matt Kantor but if there is no need to create a model for the intermediate table how do you save records into that table? – ivantxo Sep 22 '11 at 22:40
  • I have the same question as Ivan has, how to update the intermediate table? Since both ways in relation need to fetch data from the intermediate table, we need a way to insert an entry into the table, without it, how to insert new entry? – Simon Guo Oct 30 '11 at 04:15
0

I am just using Yii. Just a basic idea. If you thought One to Many is not a problem then you need to create a middle table in between like for Users and Orders, create a UsersOrders table to map those keys. Then create function to get those related tables in the class like $this->UsersOrders->Orders() for function Orders in User class, vice versa.

Many to Many is actually groundup by 3 tables. 2 tables but plus hidden table in the middle.

CallMeLaNN
  • 8,328
  • 7
  • 59
  • 74
  • right I know about the 3rd table, but im just wondering if Yii has a way to deal with Many to Many in a simple way using relationships or something – djt Mar 08 '11 at 02:42
  • Ok, if Yii does not handle it. We may extend the controller to have like $this->Orders() in Users controller to get the list of all Orders, as well as other functions to add/remove relationship. – CallMeLaNN Mar 11 '11 at 16:54
  • Have you test it? Last year, when I try playing with it, it seems not fully supported. If yes, then we should no need to think about intermediate table just like accepted answer above. It should be handle automatically. Unless Yii has fully supporting it in the latest release. – CallMeLaNN Feb 01 '12 at 15:15