Many-to-many: Defining and using many-to-many associations

Two types of records are related with a many-to-many associations if they both define matching properties with "has_many" keys.

Porte provide two strategies to internally store "many-to_many" associations:

Plural and singular conversion of the properties are automatically handled.

Methods available with a "has_many" association

-   *return PorteRecord*
The current record
-   *param mixed(PorteRecord, int, string)*
    The record witch should no longer be associated
-   *return PorteRecord*
    The current record

Differentiate the "join table" and the "foreign key column" storages

Let's take an exemple: a group can have many users, and a user can be member of different groups. There are 2 strategies to manage this kind of relationship in the database: using a join table or with a list of primary keys stored in a text column. Both strategies are implemented, it just depend on the way you declare the has_many property when configuring your properties. The codes below show the differences in the database structure.

join table

CREATE TABLE `Users` ( `id` int(11) NOT NULL auto_increment, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8

CREATE TABLE `Groups` ( `id` int(11) NOT NULL auto_increment, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8

CREATE TABLE `Groups_Users` ( `id` int(11) NOT NULL auto_increment, `group` int(11) NOT NULL, `user` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8

list of foreign keys stored in a text column

CREATE TABLE `Users` ( `id` int(11) NOT NULL auto_increment, `groups` text, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8

CREATE TABLE `Groups` ( `id` int(11) NOT NULL auto_increment, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8