List of foreign-keys: Many-to-many associations using a list of foreign keys

This strategy consist in storing in one of the database field a list of primary keys. In our groups/users exemple, we could imaging a field name users in the groups table with a value of '34,58', which means this group has users with primary keys equal to 34 and 58. If another group has a value '34,93', then the user with primary key equals to 34 is member of the 2 groups.

Storing relationships as a list of primary keys present the advantage of being easier to administer. However, keep in mind this is not the most efficient approach.

You will be using this strategy if only one side of the relationship declare an has_many association (eg groups declare to have many users, but users does not declare to have many groups) or if the 2 side declare the has_many relationship but one of the field is declared as transient.

Setting up the "has_many" property for the "list of foreign keys" strategy

Bidirectional model configuration

The following configuration:

$see samples/associations/many-to-many/create-text-column.php
$porte->models->set(array(
    'type'=>'user',
    'properties'=>array(
    'groups'=>array('has_many'))));

$porte->models->set(array(
    'type'=>'group',
    'properties'=>array(
    'users'=>array('has_many','transient'))));

Will generate the following tables:

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

Unidirectional model configuration

Because it is very slow to retrieve the associations from the side which doesn't store the foreign keys, porte offer the ability to call only the persisted side of the association.

The generated database is identical to the bidirectional mode but no methods are assigned to the transient side. For exemple, in the following exemple, the property "users" on "group" has no method.

$porte->models->set(array(
    'type'=>'user',
    'properties'=>array(
    'groups'=>array('has_many'))));

$porte->models->set(array(
    'type'=>'group',
    'properties'=>array(
    'users'=>array())));

Will print the following SQL:

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