From many to many relationships

I see all sorts of ways to handle many, many relationships in Yii. However, the examples that I see are not fully specified, and I feel that something is missing.

For example, Larry Ullman’s textbook does not use the self :: MANY_MANY relation - http://www.larryullman.com/2010/08/10/handling-related-models-in-yii-forms/

So, as for adding and retrieving records, what is the standard way to handle many for many in the model, controller, and view?

clarification: I assume Im looking for an example that includes two many-to-many tables where I can see not only both models, but also controllers and views, so I can fully understand what is happening.

+5
source share
2 answers

You really need 3 tables (so an extra model for the pivot table), but as soon as you get it, you can actually use the regular yii relationship.

For example, my project has a many-to-many relationship between purchase and transaction (please don’t ask why :)). Thus, there is a purchase model, a transaction model, and a PurchaseToTransaction model that establishes the relationship between them. I can simply execute the $ 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 the same applies for transactions, but vice versa:

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

, $oPurchase- > $oTransaction- > Yii.

" ", ( , , ).

+5

Yii. . , , , , UsersOrders . , , $this- > UsersOrders- > Orders() .

3 . 2 , .

0

All Articles