Jooctrine - Doctrine ORM in Joomla!


Published on

Easier building complex applications in Joomla! by adding a mapping layer between the domain model and the database. Some of this might be useful to improve Joomla!'s core.

While building custom Joomla!-extensions I often encountered difficulties implementing one-to-many and many-to-many relations.

Joomla! uses lists and items, where a list relates to a database table and an item to a record in that table. Because the underlying relational database is normalised, fields are atomic: contain only one value. In reality (in the domain) a person can have multiple addresses, an order can have multiple orderlines, a posting can have several comments, a product can have several images etc. Often, fields in our entities are not atomic, but can have collections of values. If we stay close to the one-to-one mapping of lists/items and database tables, as Joomla! natively does, it soon becomes messy when modelling more complex relationships. But if we use a mapping between the objects in our domain model and the database, we can easily use non-atomic fields. The same holds for inheritance mapping: a parent class and child classes can be mapped to a relational database in several ways. The Unified Content Model (UCM) is an example of class table inheritance mapping, where the superclass (core_content) has its own table, as do the subclasses. This opposed to the currently used concrete table inheritance, where every content-type maps to one table.

Fortunately this Object Relational Mapping has allready been extensively elaborated. No need to reinvent this wheel. In this presentation I'll show how I used Doctrine ORM for it. And I'll show how this could be used to improve Joomla!'s core.

Published in: Technology
1 Comment
1 Like
  • Hi,
    Presentation is very good.
    Where I can download an example with doctrine 2 integration in Joomla?

    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Jooctrine - Doctrine ORM in Joomla!

  1. 1. Doctrine ORM in Joomla!Herman Peeren, YeprJ & Beyond, June 1, 2013Jooctrine
  2. 2. Remember Flipper?
  3. 3. On the menu:1. What is Object Relational Mapping?2. Doctrine2 ORM -> Joomla! extensions3. possibilities to improve Joomla!’s core?
  4. 4. What is ORM?Object Relational Mapping
  5. 5. Mapping some knight-objects...... to their proper tables
  6. 6. • model:• JTable (& other) objects• JTableContent• WeblinksTableWeblink• JTableUser, JUser• collection• object / item (entity)• relational database:• tables• #__content• #__weblinks• #__users• table• rowSimple “mapping” in Joomla!
  7. 7. • 1NF = only atomic fieldsBUT:• user could have multiple- (email) addresses,- skills,- friends,- partners...
  8. 8. Mapping Layer• layer between the objects and the tables• mapping associations
  9. 9. Association mapping• one to one• one to many• many to manyIn relational databasetraversed with foreign keys + joinsYou get the whole thing together
  10. 10. Objects traversal:// Listing streets per customerforeach ($customers as $customer){echo $customer->getName().’<br />’;foreach ($customer->getAdresses() as $address) // Lazy Loading{echo ‘<li>’.$address->getStreet()</li>;}}
  11. 11. Collections• ArrayCollection class inDoctrineCommon (Collection interface)• Lazy vs eager loading• Transitive persistence (cascades)
  12. 12. Splitting in layers:• domain layer (the entities)• mapping layer(with persistence API)• data layer (the database)
  13. 13. Transparent persistence• the entities (objects) themselves don’t know anythingabout persistence• N.B.: JTable = Active Record= entities are table rows with CRUD
  14. 14. Unit of Workupdate ofcustomer name, address and creditlimitwith 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. 15. Defining the mappingIn Doctrine the mapping can be definedin 4 ways:• annotations• xml• yml• phpThe mapping information is stored in ClassMetadata in-stances (which are cached in a production environment)
  16. 16. ORM Designervisual mapping tool (not Free!):
  17. 17. Command line tool• db-schema generation!• direction: from mapping info to db,not the other way around!!!• and entity-generation
  18. 18. Inheritance mapping• concrete table inheritance(a.k.a.: mapped superclass)• class table inheritance(a.k.a.: joined table inheritance)• one table inheritance(a.k.a.: single table inheritance)Zie Fowler’s PEAA(images 2 next pages)
  19. 19. Concrete Table inheritance
  20. 20. Class Table inheritance
  21. 21. NoSql-mapping• Doctrine ODM (a.o. MongoDB)• easy switching ORM - ODM• mix for instance MySql and MongoDB:
  22. 22. DQLDoctrine 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, aFROM Customer c JOIN c.addresses a”);$customers = $query->getResult();
  23. 23. Doctrine2 documentation•
  24. 24. Using Doctrine2 ORMfor Joomla! extensions• installation via composer• Also see articles Paul de Raay:• Adding a prefix (see cookbook):
  25. 25. Jooctrine-package version 0.1for easy use, including:• latest Doctrine2• + bootstrap.php• + jooctrinemodel.php• + prefix listenerdownload from jooctrine.orgwill be installed in libraries/doctrine(for easy plug and play; to be continued)
  26. 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 JooctrineModelentity manager: $this->em• I use 1 model in a component (my “domainmodel”)• this model is used with several views
  27. 27. What I came acrossmaking 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
  28. 28. Change a basic building blockand ...
  29. 29. possibilities to improveJoomla!’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)
  30. 30. Questions?
  31. 31. Thank you!more info and