Doctrine2 Day 3 – Proxies, Associations, Relationships

Doctrine2 Day 3 – Proxies, Associations, Relationships

Well if you are following this series, then by know you are aware that we have the validators setup, and we are almost ready to go. Well not quite so. I ran into an issue with proxies and class loaders which took a while to resolve, but what I did was:

a) Changed the Zend Framework-Doctrine2 integration to the Bisna integration (https://github.com/guilhermeblanco/ZendFramework1-Doctrine2) – note the additional configurations before

b) Learnt that DO NOT SAVE ANY MODELS IN SESSION OR CACHES due to Proxy auto loading issues

c) Develop unit tests for the model validations and saving as you go along because they will save you as you shift things around . I am currently trying to save an association to the database but due to the tests that I have running I can test out the different association mappings without issues because I track the changes using my unit tests.

Now onto relationships, well what I found was a follows:

  1. I have been having a tough time dealing with relationships from Doctrine 1 which autoloaded relationships, however with with the Doctrine2 data mapper, you have to auto load the associations your self
  2. In Doctrine 2, you cannot define the foreign key column and relationship as this creates problems for the ORM mapper, so since I need to access the foreign key value for example personid, and the related object person, my approach has been as follows:
    • Added a person instance and relationship mapping to the person as per the Doctrine
    • Add a setter setPersonID() which loads the Person from the database using the provided ID and sets it to the person relationship provided
    • Added a getter getPersonID() which obtains the id of the person for use on the screen
  3. As recommended avoid bi-directional relationships where possible, try to keep them as uni-directional as possible.

Next I will be implementing a nested hierarchical structure using the tree nested set implementation at http://www.gediminasm.org/

One thought on “Doctrine2 Day 3 – Proxies, Associations, Relationships

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top