many-to-many creation

Hello,

is there any easy and convenient way to create many-to-many links in yii? In the provided blog example, a manual delete/insert is used:

http://code.google.c…ls/Post.php#102

That doesnt seem quite nice to me - is there any way using the provided ActiveRecord? Adding an extra model for each mn table seems awkward, too.

any ideas?

There's no other way. Either use a SQL like in the blog or create another ActiveRecord class for the link table.

Having a separate AR for ManyMany relations isn't that painful.

Just add a function to your AR class to do it, f. ex:

Team <ManyMany> Person

class Team extends CActiveRecord {


    function addPerson($person) {


        // code could be something like this


        $teamToPerson = new TeamToPerson();


        $teamToPerson->teamID = $this->ID;


        $teamToPerson->playerID = $player->ID;


        $teamToPerson->save();


    }


}


Then everywere is your code, adding a person to a team is one line of code!