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
- Key "type"
Used only in the "list of foreign keys" strategy, define the type of the field storing the list of foreign keys. By default, the "type" is "text". - Key "field"
Used only in the "list of foreign keys" strategy, define the name of the field storing the list of foreign keys. By default, it corresponds to the property name. - Key "transient"
Used only in "list of foreign keys" strategy, inform the model that this side of the association is not storing the foreign keys. The other side of the relation should not set the "transient" property or an error will be thrown. - Key "has_many"
An array defining various internal properties. It may also be a value, or its value may be a boolean, a string or an array. If the value is a string, it corresponds to the "type" of the associated record.- Key "type"
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