Active record support for many to many

I’m looking for support for many-to-many relations with active record, and ended up being a little confused what to choose. There are many ways this can be done with different extensions. Some extensions are old, one seemed OK, but I never got it to work with Yii 1.1.15 and others seems unsupported.

I hope someone can recommend solutions so that I don’t have to waste more time testing the different solutions.

Until now this is the solution that seems best: Yii 1.1: esaverelatedbehavior

What exactly you need? many-many relations are supported out of the box by AR in Yii: http://www.yiiframework.com/doc/guide/1.1/en/database.arr (look for MANY_MANY)

if you are looking for automated many_many relations persisting - I agree there is a lot of choices, mostly deprecated or discontinued. I prefer persisting such records manually, without automation…

please check this

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

1 Like

That’s what I mean. I want to specify the relation in the form view, and when the form is saved, and the controller is run to save the update, the many-to-many relation is automatically handled.

For some years I have handeled this manually, but I see the need for automatication here to save time developing. I create a lot of medium size projects from scratch, and find myself writing the same manually handling m2m relations over and over again, which could be solved by an extension.

Seems like I will go for this extension to solv my problem, if no-one has a nother suggestion.

if you can find same code across projects - write your own extensions or even simple helpers that suit your needs. What I meant by handling it manually is rather “I have my own helper class with static method that wipes and sets records in pivot table for provided master model and array of related models” :)

I wanted to finish this topic with my final solution. I chose the extension esaverelatedbehavior to have controller handle my many-to-many relation. The model has the many-to-many relation specified, in the form i have a listbox that lists all elements in the related table, and the selected elements are handled in the controller this way:




        if (isset($_POST['MyModel'])) {

                $model = new MyModel();

                $model->attributes = $_POST['MyModel'];

                if ($model->saveWithRelated('relationName')) {

                        $this->redirect(array('site/index'));

                }

        }