This strategy consist of creating a new table which responsibility is to reference each side of the relationship. if you call the tableUpdate method, the table is automatically created and its name respond to the following convention: ClassA_ClassB where ClassA < ClassB. The 2 fields created in this table respond to the following convention: class_a_id and class_b_id where id is the primary key of ClassA and ClassB.
Storing relationships with a join table is the fastest strategy. It also present the advantage of having the ability to values specific to the relationship (for exemple, we could image the date when the relationship is created). However, storing values specific to a relationship is not yet handled.
You will be using this strategy if the two sides of the relationship define an has_many field (and if none of them is marked as transient).
Setting up the "has_many" property for the "join table" strategy
The following keys may be used:
-
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.
- Key "join": Array olding properties specific to the "join table" strategy.
- Key "database": Correspond to the name of the database storing the table associations.
- Key "table": Correspond to the name of the database table storing the associations foreign keys.
- Key "field": Name of the database field storing the foreign key of the current type of record.
- Key "encoding": Encoding of the join table. Default is current table encoding.
- Key "engine": Table engine of the join table. Default is current table engine.
Quick exemple
An has_many relationship with a join table
class Groups extends PorteRecord{
$meta_fields = array(
'users' => array(
'has_many' => true
)
)
}
class User extends PorteRecord{
$meta_fields = array(
'groups' => array(
'has_many' => true
)
)
}
Calling "table->drop" method
When requesting to drop a table, the join table will be dropped as well.