This document discusses using the Doctrine ORM framework to improve object mapping in Joomla. It describes how Doctrine allows mapping between objects and database tables through associations like one-to-one, one-to-many, and many-to-many relationships. Using Doctrine's mapping features could help address Joomla's limitations with hierarchies, nested objects, and separating data structure from content models. The Jooctrine package provides an easy way to use Doctrine in Joomla extensions by handling configuration, models, and table prefixes. While Doctrine improves object mapping, fully replacing Joomla's core functionality could break many aspects that rely on its current design.
9. Association mapping
• one to one
• one to many
• many to many
In relational database
traversed with foreign keys + joins
You get the whole thing together
10. Objects traversal:
// Listing streets per customer
foreach ($customers as $customer)
{
echo $customer->getName().’<br />’;
foreach ($customer->getAdresses() as $address) // Lazy Loading
{
echo ‘<li>’.$address->getStreet()</li>;
}
}
12. Splitting in layers:
• domain layer (the entities)
• mapping layer
(with persistence API)
• data layer (the database)
13. Transparent persistence
• the entities (objects) themselves don’t know anything
about persistence
• N.B.: JTable = Active Record
= entities are table rows with CRUD
14. Unit of Work
update of
customer name, address and creditlimit
with Doctrine:
// this is done in memory, in the customer repository:
$customer->setName(‘Richy’);
$customer->setCreditlimit(10000);
// $address is an address-object, that has been set before
$customer->setAddress($address);
// and the changes are made permanent in the database
$em->flush();
15. Defining the mapping
In Doctrine the mapping can be defined
in 4 ways:
• annotations
• xml
• yml
• php
The mapping information is stored in ClassMetadata in-
stances (which are cached in a production environment)
21. NoSql-mapping
• Doctrine ODM (a.o. MongoDB)
• easy switching ORM - ODM
• mix for instance MySql and MongoDB:
http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/cookbook/
mapping-classes-to-orm-and-odm.html
22. DQL
Doctrine Query Language
• looks like SQL,
but querying the entities, not the tables
• querybuilder
// Query addresses per customer with fetch-join: eager loading
$query = $em->createQuery(“SELECT c, a
FROM Customer c JOIN c.addresses a”);
$customers = $query->getResult();
24. Using Doctrine2 ORM
for Joomla! extensions
• installation via composer
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configura-
tion.html
• Also see articles Paul de Raay:
http://www.paulderaaij.nl/tag/doctrine/
• Adding a prefix (see cookbook):
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/sql-table-
prefixes.html
25. Jooctrine-package version 0.1
for easy use, including:
• latest Doctrine2
• + bootstrap.php
• + jooctrinemodel.php
• + prefix listener
download from jooctrine.org
will be installed in libraries/doctrine
(for easy plug and play; to be continued)
26. // using Joomla config-info
$joomlaConfig = JFactory::getConfig();
$dbParams = array(
‘driver’ => ‘pdo_mysql’,
‘path’ => ‘database.mysql’,
‘charset’ => ‘utf8’,
‘host’ => $joomlaConfig->get(‘host’),
‘dbname’ => $joomlaConfig->get(‘db’),
‘user’ => $joomlaConfig->get(‘user’),
‘password’ => $joomlaConfig->get(‘password’)
);
// idem: the prefix
$prefix = $joomlaConfig->get(‘dbprefix’);
extend model from JooctrineModel
entity manager: $this->em
• I use 1 model in a component (my “domainmodel”)
• this model is used with several views
27.
28. What I came across
making a Joomla-extension with this:
• it is easy to use Doctrine in Joomla! Great possibilities!
BUT:
• change a basic building block and a lot changes
• many things don’t function anymore ...
• singular or plural view is essential in Joomla!
• also: 1 table row = 1 entity with atomic fields
30. possibilities to improve
Joomla!’s core?
• hierarchies, nested associations without JTableNested
• fields can be objects, objects can be collections
• UCM: mapping and structure separated
• stucture in model; no HMVC mis-use
• better OOP, beyond the db-normalisation paradigm
• easier Language associations (Flipper always returns)
32. Thank you!
more info and download:
www.jooctrine.org
contact:
herman@yepr.nl
www.hermanpeeren.nl
www.yepr.eu
illustrations:
www.redcheeksfactory.com