Yii framework Many to Many relationships

2019-04-05 04:54发布

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

6条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-05 05:20

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-04-05 05:20

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

查看更多
迷人小祖宗
4楼-- · 2019-04-05 05:20

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.

查看更多
乱世女痞
5楼-- · 2019-04-05 05:34

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

查看更多
smile是对你的礼貌
7楼-- · 2019-04-05 05:35

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.

查看更多
登录 后发表回答