DAO¶
The DAO class is responsible for loading and persistence operations on models :
Loading data¶
Loading an instance¶
Loading an instance of the models\User class with id 5
use Ubiquity\orm\DAO;
$user=DAO::getOne("models\User",5);
BelongsTo loading¶
By default, members defined by a belongsTo relationship are automatically loaded
Each user belongs to only one category:
$user=DAO::getOne("models\User",5);
echo $user->getCategory()->getName();
It is possible to prevent this default loading ; the third parameter allows the loading or not of belongsTo members:
$user=DAO::getOne("models\User",5, false);
echo $user->getCategory();// NULL
HasMany loading¶
Loading hasMany members must always be explicit ; the third parameter allows the explicit loading of members.
Each user has many groups:
$user=DAO::getOne("models\User",5,["groupes"]);
foreach($user->getGroupes() as $groupe){
echo $groupe->getName()."<br>";
}
Composite primary key¶
Either the ProductDetail model corresponding to a product ordered on a command and whose primary key is composite:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | namespace models;
class ProductDetail{
/**
* @id
*/
private $idProduct;
/**
* @id
*/
private $idCommand;
...
}
|
The second parameter $keyValues can be an array if the primary key is composite:
$productDetail=DAO::getOne("models\ProductDetail",[18,'BF327']);
echo 'Command:'.$productDetail->getCommande().'<br>';
echo 'Product:'.$productDetail->getProduct().'<br>';
Loading multiple objects¶
Loading instances of the User class:
$users=DAO::getAll("models\User");
foreach($users as $user){
echo $user->getName()."<br>";
}
Loading instances of the User class with his category and his groups :
$users=DAO::getAll("models\User",["groupes","category"]);
foreach($users as $user){
echo "<h2>".$user->getName()."</h2>";
echo $user->getCategory()."<br>";
echo "<h3>Groups</h3>";
echo "<ul>";
foreach($user->getGroupes() as $groupe){
echo "<li>".$groupe->getName()."</li>";
}
echo "</ul>";
}