Object Relational Mapping Layer Views Controlers Business logic GUI OO-application logic Object-relational-Mapping Relational DBMS PHP (propel) 1/18
Propel - Persistance Layer OR-Mapper für PHP Portierung von Torgue (Turbine Framework, Java) Konfiguration der Abbildung durch XML-Datei Unterstützt aktuell Oracle, MySQL, SQL-Server, PostgeSQL Generiert OR-Schicht, Datenbankschema Unterstützt Basic CRUD-Funktionalität Simuliert Foreign-Key Constraints falls nicht von DB unterstützt Criteria Klasse für Anfragen an Datenbank Validatoren für Spalten definierbar (match, nomatch, maxlength, maxvalue, minlength, minvalue, validvalues,...) Unterstützt 1:1, 1:n Beziehungen, Vererbung PHP (propel) 2/18
Ablauf der Generierung Base<x>.php extends schema.xml propel-gen (falls nicht existent) <x>.php Base<x>Peer.php extends (falls nicht existent) build.properties schema.sql <x>peer.php PHP (propel) 3/18
Reverse Engineering existing schema propel-gen./ creole schema.xml PHP (propel) 4/18
Beispiel: build.properties # The name of the project propel.project = HappySail # The database driver propel.database = mysql propel.mysql.tabletype=innodb # Connection Information propel.database.url = mysql:host=localhost;dbname=happysail propel.database.user = propeltest propel.database.password = test PHP (propel) 5/18
Beispiel schema.xml <?xml version="1.0" encoding="iso-8859-1" standalone="no"?> <database name="happysail" defaultidmethod="native"> <table name="schiff"> <column name="id" type="integer" primarykey="true" autoincrement="true"/> <column name="name" type="varchar" size="40" required="true"/> <column name="laenge" type="integer" required="true"/> <column name="plaetze" type="integer" required="true"/> <column name="hafen_fk" type="integer"/> <foreign-key foreigntable="hafen" ondelete="set null"> <reference local="hafen_fk" foreign="id"/> </foreign-key> </table> <table name="hafen"> <column name="id" type="integer" primarykey="true" autoincrement="true"/> <column name="name" type="varchar" size="40" required="true"/> <column name="ort" type="varchar" size="40" required="true"/> <column name="anlegeplaetze" type="integer"/> </table> </database> PHP (propel) 6/18
Fakultät IWI generiertes Datenbankschema #------------------------------------------------------------ #-- schiff #------------------------------------------------------------ DROP TABLE IF EXISTS `schiff`; CREATE TABLE `schiff` ( `id` INTEGER NOT NULL AUTO_INCREMENT, `name` VARCHAR(40) NOT NULL, `laenge` INTEGER NOT NULL, `plaetze` INTEGER NOT NULL, `hafen_fk` INTEGER, PRIMARY KEY (`id`), INDEX `schiff_fi_1` (`hafen_fk`), CONSTRAINT `schiff_fk_1` FOREIGN KEY (`hafen_fk`) REFERENCES `hafen` (`id`) ON DELETE SET NULL )Type=InnoDB; #------------------------------------------------------------ #-- hafen #------------------------------------------------------------ DROP TABLE IF EXISTS `hafen`; CREATE TABLE `hafen` ( `id` INTEGER NOT NULL AUTO_INCREMENT, `name` VARCHAR(40) NOT NULL, `ort` VARCHAR(40) NOT NULL, `anlegeplaetze` INTEGER, PRIMARY KEY (`id`) )Type=InnoDB; PHP (propel) 7/18
generierte Klassen CRUD Klassen (werden immer neu generiert) build/classes/happysail/om/basehafen.php build/classes/happysail/om/basehafenpeer.php build/classes/happysail/om/baseschiff.php build/classes/happysail/om/baseschiffpeer.php Applikationslogik Stubs (werden nur beim ersten mal generiert) build/classes/happysail/hafen.php build/classes/happysail/hafenpeer.php build/classes/happysail/schiff.php build/classes/happysail/schiffpeer.php PHP (propel) 8/18
BaseSchiff.php Variablenübersicht: $ grep protected build/classes/happysail/om/baseschiff.php grep -v function protected static $peer; protected $id; protected $name; protected $laenge; protected $plaetze; protected $hafen_fk; protected $ahafen; protected $alreadyinsave = false; protected $alreadyinvalidation = false; protected $validationfailures = array(); Foreign Key auf Hafen Datensatz Hafen Instanz alle Zeilen in denen das Wort protected vorkommt alle Zeilen in denen das Wort function nicht vorkommt PHP (propel) 9/18
Fakultät IWI BaseSchiff.php Methodenübersicht: $ grep function build/classes/happysail/om/baseschiff.php public function construct() public function getid() public function getname() public function getlaenge() public function getplaetze() public function gethafenfk() public function setid($v) public function setname($v) public function setlaenge($v) public function setplaetze($v) public function sethafenfk($v) public function delete(propelpdo $con = null) public function save(propelpdo $con = null) protected function dosave(propelpdo $con) public function getvalidationfailures() public function validate($columns = null) public function getprimarykey() public function setprimarykey($key) public function copyinto($copyobj, $deepcopy = false) public function copy($deepcopy = false) public function getpeer() public function sethafen(hafen $v = null) public function gethafen(propelpdo $con = null) PHP (propel) 10/18
Fakultät IWI BaseSchiffPeer.php $ grep function build/classes/happysail/om/baseschiffpeer.php... public static function addselectcolumns(criteria $criteria) public static function docount(criteria $criteria, $distinct = false, PropelPDO $con = null) public static function doselectone(criteria $criteria, PropelPDO $con =null) public static function doselect(criteria $criteria, PropelPDO $con = null) public static function doselectstmt(criteria $criteria, PropelPDO $con = null) public static function populateobjects(pdostatement $stmt) public static function docountjoinhafen(criteria $criteria, $distinct =false, PropelPDO $con = null, $join_behavior = Criteria::LEFT_JOIN) public static function doselectjoinhafen(criteria $c, $con = null, $join_behavior = Criteria::LEFT_JOIN) public static function docountjoinall(criteria $criteria, $distinct = false, PropelPDO $con = null, $join_behavior = Criteria::LEFT_JOIN) public static function doselectjoinall(criteria $c, $con = null, $join_behavior = Criteria::LEFT_JOIN) public static function doinsert($values, PropelPDO $con = null) public static function doupdate($values, PropelPDO $con = null) public static function dodeleteall($con = null) public static function dodelete($values, PropelPDO $con = null) public static function dovalidate(schiff $obj, $cols = null) public static function retrievebypk($pk, PropelPDO $con = null) public static function retrievebypks($pks, PropelPDO $con = null)... PHP (propel) 11/18
Hafen.php <?php require 'HappySail/om/BaseHafen.php'; /** * Skeleton subclass for representing a row from the 'hafen' table. * @package HappySail */ class Hafen extends BaseHafen { /** * Initializes internal state of Hafen object. * @see parent:: construct() */ public function construct() { // Make sure that parent constructor is always invoked, since that // is where any default values for this object are set. parent:: construct(); } } // Hafen PHP (propel) 12/18
HafenPeer.php <?php require 'HappySail/om/BaseHafenPeer.php'; /** * Skeleton subclass for performing query and update operations on the 'hafen' table. * You should add additional methods to this class to meet the * application requirements. This class will only be generated as * long as it does not already exist in the output directory. * * @package HappySail */ class HafenPeer extends BaseHafenPeer { } // HafenPeer PHP (propel) 13/18
Beispielprogramm Propel::init("./build/conf/HappySail-conf.php"); $hafen1 = new Hafen(); $hafen1->setname("marina Bas du Fort"); $hafen1->setort("point a Pitre (Guadeloupe)"); $hafen1->setanlegeplaetze(450); $hafen1->save(); $hafen2 = new Hafen(); $hafen2->setname("marine de Cocolin"); $hafen2->setort("cogolin"); $hafen2->setanlegeplaetze(760); $hafen2->save(); $schiff1 = new Schiff(); $schiff1->setname("fury"); $schiff1->setlaenge("12.5"); $schiff1->setplaetze(7); $schiff1->sethafen($hafen1); $schiff1->save(); PHP (propel) 14/18
Beispielprogramm (fortsetzung) $schiff2 = new Schiff(); $schiff2->setname("zampallo III"); $schiff2->setlaenge("10.5"); $schiff2->setplaetze(6); $schiff2->sethafen($hafen2); $schiff2->save(); $schiff3 = new Schiff();... echo "\nalle Schiffe mit Heimathafen Cocolin:\n"; $list = $hafen2->getschiffs(); foreach ($list as $ship) echo " ".$ship->getname()."\n"; echo "\nalle Schiffe mit mehr als 6 Plätzen\n"; $crit = new Criteria(); $crit->add(schiffpeer::plaetze,6,criteria::greater_than); foreach (SchiffPeer::doSelect($crit) as $s) echo " ",$s->getname()," (",$s->getplaetze()," Plätze) Heimathafen: ", $s->gethafen()->getname()."\n";?> PHP (propel) 15/18
c:/programme/php/php.exe test-run.php Alle Schiffe mit Heimathafen Cocolin: Zampallo III 13 pfeifende Schweine Alle Schiffe mit mehr als 6 Plätzen Fury (7 Plätze) Heimathafen: Marina Bas du Fort 13 pfeifende Schweine (9 Plätze) Heimathafen: Marine de Cocolin PHP (propel) 16/18
Datenbankeinträge $ c:/programme/mysql/bin/mysql.exe -u root HappySail mysql> select * from schiff; +----+-----------------------+--------+---------+----------+ id name laenge plaetze hafen_fk +----+-----------------------+--------+---------+----------+ 18 Fury 12 7 13 19 Zampallo III 10 6 14 20 13 pfeifende Schweine 16 9 14 +----+-----------------------+--------+---------+----------+ 3 rows in set (0.00 sec) mysql> select * from hafen; +----+--------------------+----------------------------+---------------+ id name ort anlegeplaetze +----+--------------------+----------------------------+---------------+ 13 Marina Bas du Fort Point a Pitre (Guadeloupe) 450 14 Marine de Cocolin Cogolin 760 +----+--------------------+----------------------------+---------------+ 2 rows in set (0.00 sec) PHP (propel) 17/18
Literaturhinweise http://www.propelorm.org/documentation/ Anderer bekannter OR-Mapper für PHP: http://www.doctrine-project.org/ PHP (propel) 18/18