Yii framework Many to Many relationships

自闭症网瘾萝莉.ら 提交于 2019-12-04 01:13:54

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.

user318750

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

mjalajel

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!