Yii framework Many to Many relationships
Asked Answered
G

6

11

What is the method to save and update Many to Many relationship in Yii framework?

Gannes answered 26/9, 2009 at 13:26 Comment(0)
V
3

Unless you create a model for the table between the two main tables, your only option is to use DAO (Database Access Object) and specify SQLs with it.

Have a look at how blog demo accomplishes this task.

Vacate answered 6/10, 2009 at 17:20 Comment(0)
C
3

use MANY_MANY relationship type to setup many to many connection between Models (An associative table is needed to break a many-to-many relationship into one-to-many relationships) And now you can use all relational functions of Active Records

Yii Framework - The Definitive Guide to Yii: Working with Databases-Relational Active Record

Commutual answered 23/4, 2010 at 2:39 Comment(0)
S
1

The following extension does what you want... Yii Framework - Extension: cadvancedbehavior

An important thing to note: On each update, the extension clears all previous records and creates new ones. So I wouldn't use it when the intermediatry table contains extra data other than the foreign keys.

Santiago answered 25/5, 2012 at 20:38 Comment(0)
H
0

you could set that up in mysql level..by going to relational view under each table in phpmyadmin and provide necessary relational condition..and use MANY_MANY in the model class inside relations..

Hogg answered 14/2, 2012 at 9:2 Comment(0)
O
0

The question is too common.

Usually data components with MANY to MANY relationships appear sequentially and independently. So you just need to do one insert action after another.

If your relationship needs dependent update you should user SQL triggers on the DataBase level. That'll ensure integrity of data and give a quite good separation in business logic of the application.

CREATE TRIGGER some_trigger
    AFTER UPDATE ON some_table
    ...
END IF;

A similar way is to incapsulate relational data in one logical model on PHP level (and e.g. manipulate with 2-3 AR models there) and emulate SQL triggers logic in it.

Operant answered 20/7, 2012 at 15:33 Comment(2)
Triggers are horrible for maintenance. First you might consider using database fks and cascading... triggers might be really the last option.Kurrajong
You're must be joking, aren't you? FKs cascading are triggers indeed! But you cannot use them "on insert". Moreover the MyIsam engine of MySql doesn't support FKs e.g. As concerns maintenence, I don't know what is the problem to maintain a DB procedure especially if it's a trivial (and is alike a FK cascading)Operant

© 2022 - 2024 — McMap. All rights reserved.