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, as string or an array. If the value is a string, it corresponds to the "type" of the
associated record.
- Key "type": Define the type of the associated records
- Key "class": Define the class of the associated record, used as an alternative to define the "type" of records if not set
- Key "property": Define the name of the property defining the "belongs_to" association, default to current record type.
Quick exemple
An has_many relationship defined on one side only
class Groups extends PorteRecord{
$meta_fields = array(
'users' => array(
'has_many' => true
)
)
}
class User extends PorteRecord{
$meta_fields = array(
)
}
An has_many relationship defined on two sides with a transient field
class Groups extends PorteRecord{
$meta_fields = array(
'users' => array(
'has_many' => true
)
)
}
class User extends PorteRecord{
$meta_fields = array(
'groups' => array(
'has_many' => true,
'transient' => true
)
)
}