One-to-one: Defining and using one-to-one associations
Available methods
Both sides of the association recieve the same methods.
- Method set{Property}
Associated record may be an object (instance of PorteRecord), an int (or a string casted to an int), an array (representing the record, like the return value of the "record->toArray" method) or null (if property not tagged as not_null). - return PorteRecord: Current record
- param PorteRecord mixed[optional]: Associated record as a record, primary key, array or null; default to null of not provided
- Method get{Property}
Return the associated record or null if not defined.- return mixed: PorteRecord if association is defined or null
- Method delete{Property}
Break the association if one existed. - return PorteRecord: Current record
Quick exemple
Exemple using the "belongs_to" and the "has_one" properties:
class User extends PorteRecord{
public $config = array(
'properties' => array(
'person' => array( 'belongs_to' )
)
);
}
class Person extends PorteRecord{
public $config = array(
'properties' => array(
'user' => array( 'has_one' )
)
);
}
$connection->users->update(array('drop'));
$connection->persons->update(array('drop'));
$user = new User();
$person = new Person();
$user->setPerson($person);
$user->save();
echo 'User id is: '.$user->getId()."\r\n";
echo 'Person id is: '.$person->getId()."\r\n";
echo 'Person id associated to user is: '.$user->getPerson()->getIdentifier()."\r\n";
$person->deleteUser();
echo 'Person id associated to user is: '.(is_null($user->getPerson())?'none':'error')."\r\n";
// Will output:
//
// User id is: 1
// Person id is: 1
// Person id associated to user is: 1
// Person id associated to user is: none
Setting up the "belongs_to" property
The following keys may be used:
- Key "field": Define the database field storing the primary key of the associated record, default to the property name.
- Key "belongs_to": An array defining various internal properties. Also, it may be a value, or its value may be a boolean, a string or an array. If the value is a string, it correspond to the "type" of the associated record.
- Key "type": Define the type of the associated record
- 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 plural record type.
Once a property is clean up by its model, the resulting config is:
- Key "belongs_to": always an
- Key "type"
- Key "class"
- Key "property"
- Key "field"
- Key "type"
- Key "foreign_key": Boolean value set to true
- Key "lazy": Boolean value set to true
Setting up the "has_one" property
The following keys may be used:
- Key "has_one": 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": 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.
Once a property is clean up by its model, the resulting config is:
- Key "has_one": always an array
- Key "type"
- Key "class"
- Key "property"
- Key "transient": Mark the property as not persisted in the database
- Key "lazy": Boolean value set to true