Der Vortrag "Zend Framework meets Doctrine 2" von Paul Seiffert erklärt die Verwendung von Doctrine 2 in Projekten, die auf Zend Framework (ZF) basieren. Zuerst wird Paul dabei auf das Doctrine 2 ORM und dessen
grundsätzliche Verwendung eingehen (es wird also kein Doctrine 2 - Wissen vorausgesetzt), im weiteren Verlauf wird er die Einbindung des ORMs in ZF-Projekte erklären. Zum Schluss zeigt Paul ein kleines Beispiel-Projekt, in dem ZF und Doctrine 2 gemeinsam zum Einsatz kommen.
2. Paul Seiffert
I Developer @ Mayflower seit
September 2010
I PHP seit ca. 8 Jahren
I Mobile Application Development
I B.Sc. Computer Science (TU
München)
I facebook.com/seiffertp
Mayflower GmbH I 2
3. Agenda
1. Doctrine 2
i. Übersicht
ii. Architektur
iii. Begriffe
2. Zend Framework Integration
3. Show Case
Mayflower GmbH I 3
4. Agenda
1. Doctrine 2
i. Übersicht
ii. Architektur
iii. Begriffe
2. Zend Framework Integration
3. Show Case
Mayflower GmbH I 4
5. Doctrine
I PHP-ORM
→ geschrieben in PHP
→ geschrieben für PHP
I Mapping von Objekten der
Business-Logik zu Datenbank-Records
I Mapping von Klassen der Business-Logik zu Datenbank-Tabellen
I API basiert auf Hibernate (Java)
I Repository- und Row-Data-Gateway-Pattern
Mayflower GmbH I 5
6. Doctrine 2
2
I Komplette Neu-Entwicklung
I Im Vergleich zu Doctrine (1):
→ Weniger Magie
→ Um Welten performanter
→ Besseres Caching
→ Bessere Erweiterbarkeit
→ 100% PHP 5.3
Mayflower GmbH I 6
7. Doctrine 2 - Architektur
Applikationen
ORM
DBAL
Common
Mayflower GmbH I 7
8. Doctrine 2 – Entities I
I Plain Old PHP Objects
I Vererbung
→ Single Table Inheritance
→ Class Table Inheritance
I Aggregation / Composition
→ Management von 1:1, 1:n und n:m Relationen
Mayflower GmbH I 8
9. Doctrine 2 – Entities II
namespace MyProjectEntity;
class User
{
protected $_id;
protected $_username;
protected $_email;
Beispiel?
public function getId()
{
return $this->_id;
}
...
}
Mayflower GmbH I 9
10. Doctrine 2 – EntityManager I
I Zentraler Zugriff auf die ORM-Funktionalität
→ Findet Entities
→ Persistiert Entities
→ Stellt Entity-Repositories zur Verfügung
I Aggregiert Änderungen und schreibt diese in Paketen (Unit of
Work) zurück in die DB
Mayflower GmbH I 10
11. Doctrine 2 – EntityManager II
Entity 1
Entity
DB
Manager
Entity 2
Mayflower GmbH I 11
12. Doctrine 2 – EntityManager III
// $em is the EntityManager
$peter = $em->find('MyProjectEntityUser', 5);
$peter->setEmail('peter@example.com');
$em->flush();
$paul = new User();
$paul->setUsername('Paul');
$paul->setEmail('paul@example.com');
$em->persist($paul);
$em->flush();
Mayflower GmbH I 12
13. Doctrine 2 – Repositories I
I Pro Entity-Klasse ein Repository
I API zur Suche nach Entities
→ find, findBy, findBy<attributeName>
→ eigene Such-Methoden
I spezielle Funktionalität durch
Custom Repositories
Mayflower GmbH I 13
14. Doctrine 2 – Repositories II
Client
findMembersOfGroup(group) User
Repository
find(3)
find('User', 3)
Entity
DB
Manager
Mayflower GmbH I 14
16. Doctrine 2 – Metadata
I Mapping-Anweisungen (Metadaten) beschreiben
→ Die persistente Struktur der Daten
→ Die Beziehungen zwischen Klassen
I Metadaten können unterschiedlich vorliegen:
→ XML
→ YAML
→ PHP
→ DocBlock Annotations
Mayflower GmbH I 16
21. Doctrine 2 ...
… es gibt noch so viel zu lernen!
I RTFM:
http://www.doctrine-project.org/docs/orm/2.1/
Mayflower GmbH I 21
22. Agenda
1. Doctrine 2
i. Übersicht
ii. Architektur
iii. Begriffe
2. Zend Framework Integration
3. Show Case
Mayflower GmbH I 22
23. Integration von Doctrine 2 in ZF-Projekte
I Doctrine 2 wird als fester Bestandteil von Symfony 2 entwickelt
I Doctrine 2 ist jünger als Zend Framework (1).
I Aktuell: ZF 1.11.11, Doctrine 2.1
I Doctrine 2 basiert auf PHP 5.3
→ Namespaces
Mayflower GmbH I 23
24. Integration – Konfiguration der Classloader
use DoctrineCommonClassLoader as DoctrineClassLoader;
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initDoctrineClassLoader()
{
$classLoader = new DoctrineClassLoader('Doctrine');
$classLoader->register();
return $classLoader;
}
public function _initSymfonyClassLoader()
{
$classLoader = new DoctrineClassLoader('Symfony');
$classLoader->register();
return $classLoader;
}
}
Mayflower GmbH I 24
29. CLI Tool
Doctrine Command Line Interface version 2.2.0-DEV
...
Available commands:
That's it!
help Displays help for a command
list Lists commands
dbal
dbal:import Import SQL file(s) directly to Database.
dbal:run-sql Executes arbitrary SQL directly from the command line.
orm
orm:clear-cache:metadata Clear all metadata cache of the various cache drivers.
orm:clear-cache:query Clear all query cache of the various cache drivers.
orm:clear-cache:result Clear all result cache of the various cache drivers.
orm:convert-d1-schema Converts Doctrine 1.X schema into a Doctrine 2.X schema.
orm:convert-mapping Convert mapping information between supported formats.
orm:ensure-production-settings Verify that Doctrine is properly configured for a production environment.
orm:generate-entities Generate entity classes and method stubs from your mapping information.
orm:generate-proxies Generates proxy classes for entity classes.
orm:generate-repositories Generate repository classes from your mapping information.
orm:info Show basic information about all mapped entities
orm:run-dql Executes arbitrary DQL directly from the command line.
orm:schema-tool:create Processes the schema and either create it directly on EntityManager
Storage Connection or generate the SQL output.
orm:schema-tool:drop Drop the complete database schema of EntityManager Storage Connection or
generate the corresponding SQL output.
orm:schema-tool:update Executes (or dumps) the SQL needed to update the database schema to match
the current mapping metadata.
orm:validate-schema Validate the mapping files.
Mayflower GmbH I 29
30. Integration – CLI
I Bootstrap von ZF-Applikation notwendig!
→ Anpassung des Standard Doctrine-CLI-Scripts
// Erstellen der Zend_Application und Bootstrapping
/**
* @var DoctrineORMEntityManager $entityManager
*/
$entityManager = Zend_Registry::get('em');
$helperSet = new SymfonyComponentConsoleHelperHelperSet(
array('db' => new DoctrineDBALToolsConsoleHelperConnectionHelper(
$entityManager->getConnection()),
'em' => new DoctrineORMToolsConsoleHelperEntityManagerHelper(
$entityManager)));
DoctrineORMToolsConsoleConsoleRunner::run($helperSet);
Mayflower GmbH I 30
31. Agenda
1. Doctrine 2
i. Übersicht
ii. Architektur
iii. Begriffe
2. Zend Framework Integration
3. Show Case
Mayflower GmbH I 31
32. Vielen Dank für Eure Aufmerksamkeit!
Referent Paul Seiffert
Paul.Seiffert@mayflower.de
+49 89 242054 1172
Mayflower GmbH
Mannhardtstrasse6
80538 München
19.12.11 Mayflower GmbH 32
33. Literatur
I Doctrine 2 Documentation
http://www.doctrine-project.org/docs/orm/2.1/en/
I Vortrag „Doctrine 2 – Not the same Old PHP ORM“ von Jonathan H.
Wage
Mayflower GmbH I 33