Chris
Chris

Reputation: 8968

Yii framework Many to Many relationships

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

Upvotes: 11

Views: 19759

Answers (6)

mjalajel
mjalajel

Reputation: 2201

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.

Upvotes: 1

user318750
user318750

Reputation: 386

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

Upvotes: 3

Alexander Palamarchuk
Alexander Palamarchuk

Reputation: 879

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.

Upvotes: 0

Praveen Puglia
Praveen Puglia

Reputation: 5631

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..

Upvotes: 0

pestaa
pestaa

Reputation: 4749

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.

Upvotes: 3

Related Questions