There is a table of laws tbl_law . There are two other tables: the tbl_cat categories and the tbl_group groups. A law may consist of several categories as well as several groups, and vice versa a single category or group may have several laws (many to many).

I need to make a mark. form to fill:

  1. law id
  2. category id
  3. group id

In this case, the category id added to the link table tbl_cat_law , and the group id is added to the link table tbl_group_law .

It turns out that the id law should be added at once to two data models (link table tbl_cat_law and tbl_group_law ). How to do it on YII ?

Thanks to the kind people.

    2 answers 2

    So I propose a solution in the forehead, we have 3 tables and 2 binders ( tbl_cat_law tbl_group_law ) We tbl_cat_law tbl_group_law data from the form and in the method we send this data first to 1 connecting and then 2 to the second simply passing the necessary data to each model. Well, then I think it is clear. Sorry but without an example.

      Visualization of the answer @Flur (Example: linking tbl_law and tbl_cat )

      Create a tbl_cat_law table tbl_cat_law just three fields: id (Primary key) , cat_id (foreign_key: tbl_law.law_id [ON UPDATE и ON DELETE - CASCADE]) , law_id (foreign_key: tbl_cat.law_id [ON UPDATE и ON DELETE - CASCADE])
      As a result, we have a table with referencing a law-category, entries from which are changed / deleted when changing / deleting a law or category

       class Law extends CActiveRecord{ //... public functions relations(){ 'cat' => array(self::MANY_MANY, 'Cat', 'tbl_cat_law(law_id, cat_id)'), //... } //... } 

      Next, we use the link like all other links: Law::model()->with('cat')->findAll();