Getting new record objects
There exists two commonly used methods to create new records object. The first method consists in create in new instance of the record class and then optionnaly setting a connection object to it. The second method consist of using the factory pattern through the connection object.
Creating a new instance of the record class
Creating a new instance is the most natural approach to PHP developer. The important point to remember when using this technique is to link the newly created record with the connection.
The technique is a 2 step process, unless your making use of the static configuration to store the connection:
- 1. Create the record instance: "$record = new {RecordClass}()"
- 2. Set the connection (facultative): "$record->setConnection($connection)"
Note, setting the connection to the PorteConfig:$connection static property disable the need to associate a record with a connection but limit the usage of only one connection per PHP process.
Exemple by creating a new instance
Command: "php samples/load-find/new/class.php"
$user1 = new User();
$user1->setConnection($connection);
echo 'id of 1st user id: ' .$user1->save()."\r\n";
PorteConfig::$connection = $connection;
$user2 = new User();
echo 'id of 2nd user id: '.$user2->save()."\r\n";
// echo id of 1st user is: 1
// echo id of 2nd user is: 2
Using the connection
It is possible to create new instances of "PorteRecord" class directly from the connection by by getting a property or calling a method using the following naming conventions:
- Property: "$connection->{record_type}"
- Method: "$connection->{record_type}()" or "$connection->get{RecordType}()"
Exemple using the connection object
Command: "php samples/load-find/new/connection.php"
$user1 = $connection->user;
echo 'id of 1st user id: ' .$user1->save()."\r\n";
$user2 = $connection->user();
echo 'id of 2nd user id: '.$user2->save()."\r\n";
$user3 = $connection->getUser();
echo 'id of 3rd user id: '.$user3->save()."\r\n";
// echo id of 1st user is: 1
// echo id of 2nd user is: 2
// echo id of 3rd user is: 3