Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii framework Many to Many relationships

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

like image 442
Chris Avatar asked Sep 26 '09 13:09

Chris


People also ask

What is model Yii?

Yii implements two kinds of models: Form models and active records. They both extend from the same base class, CModel. A form model is an instance of CFormModel. Form models are used to store data collected from user input. Such data is often collected, used and then discarded.

What is ORM in Yii?

Yii Query Builder offers an object-oriented method for building SQL queries, which helps reduce risk of SQL injection attacks. And Yii Active Record (AR), implemented as a widely adopted Object-Relational Mapping (ORM) approach, further simplifies database programming.


3 Answers

There is a better implementation as behavior.

http://www.yiiframework.com/forum/index.php?/topic/6905-please-test-my-ar-enhancement-automatically-sync-many-many-table-when-calling-save/

like image 166
Chris Avatar answered Oct 08 '22 22:10

Chris


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.

like image 43
pestaa Avatar answered Oct 08 '22 22:10

pestaa


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

like image 45
user318750 Avatar answered Oct 08 '22 21:10

user318750