PhpMyObject manuel
Date de publication : 05/09/2007 , Date de mise à jour : 05/09/2007
Par
Nicolas BOITEUX
PMO is an API developped in PHP. It's an Object relational Mapping (ORM) wich translate database results of SQL request to objets. PMO implements Active record design pattern. PMO can manipulate 1:1, 1:n, n:m relations.
I. Introduction
II. Global Concept
III. Global working: PMO_MyObject
IV. Global working: PMO_MyMap
V. Global Working: class_loader
VI. First step with an employe table
I. Introduction
Source wikipedia: "En génie logiciel, le patron de conception (design pattern) active record est une approche pour lire les données d'une base de données. Les attributs d'une table ou d'une vue sont encapsulés dans une classe. Ainsi l'objet, instance de la classe, est lié à un tuple de la base. Après l'instanciation d'un objet, un nouveau tuple est ajouté à la base au moment de l'enregistrement. Chaque objet récupère ses données depuis la base; quand un objet est mis à jour, le tuple auquel il est lié l'est aussi. La classe implémente des accesseurs pour chaque attribut."
This manual describes PMO instances, class, méthods of the version 0.10. If you want to use PMO, you must use PHP 5.2 with the PDO module.
II. Global Concept
Description of the several steps to use PMO
- your application do an SQL request through PMO
- PMO execute this request on your SGBD
- PMO return a map referencing generic PMO objects
PMO treat the SGBD result as this
PMO cut each row of the result in several objects corresponding to the database tables in the SQL request.
Each attributes of the tables become objects attributes.
| example with users table |
SELECT * from users LIMIT 10;
will return 10 users objects.
|
| example with users and town tables |
SELECT * from users,town WHERE users.id_town=town.id_town limit 10;
|
III. Global working: PMO_MyObject
PMO instanciate objects of type PMO_MyObject. Thoses objects are not simple containers,
they inherit of methods that permits to manipulate data.
Thoses PMO_MyObject are implementations of Object Interface. You can develop your own object in implementing the object interface.
PMO use a loader mecanism that permits to load automatically your objects at interpretation time.
PMO_MyObject owns three propriety:
- object_id : uniq object id
- object_table : a ref to PMO_MyTable object wich describe the source table of object
- object_attribute : the database structure of your object wich contains the attribute name (colonns of your database) and valor corresponding
PMO_MyObject owns severals methods:
- ::factory(tablename) : to instanciate PMO_MyObject (load by class loader at interpretation time)
- delete() : delete the row in database corresponding to the object
- commit() : write the row in database corresponding to the object
| get attribute valor of an user object |
|
| modify the login attribute of the user object |
$user->login = "newlogin";
|
| write object user in the SGBD |
|
| delete the user object in the SGBD |
|
IV. Global working: PMO_MyMap
PMO keep all the PMO_MyObject references in a PMO_MyMap object. PMO_MyMap implements PMO_Map interface.
PMO_MyMap is a array map structure, and methods for retrieve easly the PMO_MyObject references.
| PMO_MyMap structure |
MyMap -|
|-- row id--|
|-- tablename --> Object reference
|
| user and town tables example |
SELECT * from user,town WHERE user.id_town=town.id_town; will return data
that can represents as this
MyMap -|
|--row result 0 --|
|-- user --> user object reference
|-- town --> town object reference
|--row result 1 --|
|-- user --> user object reference
|-- town --> town object reference
|
PMO_MyMAP methodes
- fetchMap(): fetch map row by row
- getObjectByValue(tablename, attributename, value): to retrieve an object in map functions its attribute=value.
- getMapByTable(tablename): Retrieve a map issue of the main map contains only "tablename" object
- getMapByValue(table, attributename, value): retrieve a map of objects corresponding to attribute=value.
- getMap(): return MAP array
| retrieve row by row |
$result = $map->fetchMap();
|
| return a user object with login = toto |
$object = $map->getObjectByValue("user", "login", "toto");
|
| create a secondary map containing only users objects |
$newmap = $map->getMapByTable("users");
|
| create a map only corresponding to toto objects |
$newmap = $map->getMapByValue("users", "login", "toto");
|
V. Global Working: class_loader
Class loader permits to extend objects PMO_MyObjects capacity. Your own object will inherit of PMO_MyObject and of your own methods.
To use class loader, you must create a file in the class_load/ directory with the name "class_yourtablename.php".
This file should describe your new class.
| user class method with an helloworld() |
class user extends PMO_MyObject{
public function helloworld(){
echo('this class is instanciate when a database table user exist');
}
}
methode can be call as this:
$object->helloworld();
|
VI. First step with an employe table
For following examples, i will use employe table but it should works with your tables
Unzip PhpMyObject archive, and configure config.php file.
|
$driverz = 'pdo';
$pdodriverz = 'mysql';
$hostz = 'localhost';
$userz = 'votrelogin';
$passz = 'votrepass';
$basez = 'votrebase';
|
Print of 20 employes name
require_once("core/PMO_MyController.php");
$controler = new PMO_MyController();
$map = $controler->queryController("SELECT * FROM employe limit 20 ;");
while ($result = $map->fetchMap()){
echo($result['employe']->name);
}
|
Print of 20 employes name, and their car number place.
require_once("core/PMO_MyController.php");
$controler = new PMO_MyController();
$map = $controler->queryController("SELECT * FROM employe,parking WHERE employe.id_employe=parking.id_employe limit 20 ;");
while ($result = $map->fetchMap()){
echo($result['employe']->name);
echo($result['parking']->place_number);
}
|
Modification of a name
require_once("core/PMO_MyController.php");
$controler = new PMO_MyController();
$map = $controler->queryController("SELECT * FROM employe ;");
$employe = $map->getObjectByValue("employe", "name", "dupont");
$employe->name = "durand";
$employe->commit();
|
another way to do the same thing but better
require_once("core/PMO_MyController.php");
$controler = new MyController();
$map = $controler->queryController("SELECT * FROM employe WHERE name="dupont" ;");
$employe = $map->fetchMap();
$employe->name = "durand";
$employe->commit();
|
user Object creation
require_once("core/PMO_MyController.php");
$employe = PMO_MyObject::factory("employe");
$employe->name = "Masson";
$employe->commit();
|
Print of all the film in where dupond was an actor
require_once("core/PMO_MyController.php");
$controler = new PMO_MyController();
$map = $controler->queryController("SELECT * FROM film,actor WHERE film.actor_id=actor.actor_id ;");
$actor = $map->getObjectByValue("actor", "name", "dupont");
while ($result = $actor->film->fetchMap()){
echo($result['film']->film_name);
}
|


Les sources présentées sur cette page sont libres de droits,
et vous pouvez les utiliser à votre convenance. Par contre, la page de présentation
constitue une oeuvre intellectuelle protégée par les droits d'auteurs. Copyright ©
29/07/2007 Nicolas BOITEUX. Aucune reproduction,
même partielle, ne peut être faite de ce site et de l'ensemble de son contenu :
textes, documents, images, etc sans l'autorisation expresse de l'auteur.
Sinon vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E
de dommages et intérêts.
Cette page est déposée à la
SACD.