Doctrine Relations and Data Fixtures

December 8th, 2009

This episode of zendcasts will cover how we can write Data Fixtures and extend our data model to include a simple one-to-many using Doctrine’s schema.yml file. I also noticed in the code of my last example that Model loading wasn’t working properly. I cover the fix in the video, however I’d like to highlight it here too. In your _initDoctrine() method, make sure you load the models:
Doctrine::loadModels($doctrineConfig['models_path']);

also, I’d like to extend a welcome to the folks at ServerGrove Networks for sponsoring this episode of Zendcasts. ServerGrove specializes in Zend Framework hosting and they’ve offered a 10% rebate on hosting with coupon code “zc“. If you’re looking for a host, be sure to check them out.

Grab a copy of the project or browse the repository.

Edit: I spoke to Guilherme Blanco (one of the core developers behind Doctrine) and he kindly brought up some things I should bear in mind for my Doctrine / Zend set.
Doctrine’s autoloader configuration can be scaled back by doing PEAR style Model Loading (new to 1.2). Also, you can use Doctrine_Core instead of Doctrine since everything has been moved to Doctrine_Core in order to follow Zend-style namespacing.

lastly, you can register your own CLI tasks as part of Doctrine’s CLI script or just write them in the scripts/doctrine.php file. With this, you could have your newly-generated models placed in the necessary folders. I’ll be experimenting with this in the coming week.

 

discuss video in the forum

21 Responses to “Doctrine Relations and Data Fixtures”

  1. Enkhbilguun Erdenetsogt says:

    Thank you Jon. Everyday I check ZendCasts although I know that you post once in a week because I really like your tutorials plus learnt many from you.

    I would like to ask a question after watching last 3 tutorials and as I don’t know nothing about Doctrine.

    Zend Framework Db classes seem very very good. So why do we need to use Doctrine for creating tables or retrieving datas from db? Isn’t Zend_Db simplier, faster, reliable than Doctrine on Zend Framework? Please undestand this is not offence. I just want to realize where I can use Doctrine and why it is better than Zend_Db/what is the advantage.
    Right now, within my knowledge in PHP and Zend_Db, Doctrine is useless unless the developer want to make his/her web application installer.

    Could you help me to clearly understand why Doctrine?

  2. jon says:

    Hi Enkhbilguun,
    Zend_Db still leaves its users working with SQL and forced to build a lot of models by hand. Doctrine streamlines a lot of that Create / Read / Update / Delete code, leaving you with a lot less code to write. In terms of speed, this is debatable and usually a poor excuse for picking something that is less mature (I’m referring to Zend_Db). In terms of reliability, Doctrine is a core piece of the Symfony framework and isn’t going anywhere since it follows the same design and architecture as the Java Persistence Architecture. Zend_Db takes a different approach, where users are left with a little more fine-graind control of their objects via the table-data-gateway pattern. I would argue that this isn’t a huge gain since the mapping of related objects and building up joins is usually the biggest headache with persistence tools (like MySQL) and their PHP object counterparts. Doctrine resolves a lot of these issues.

    Also, as of 2.0 of Doctrine and Zend, there will be *supported* interoperability between these two frameworks.

  3. Enkhbilguun Erdenetsogt says:

    Hello Jon,
    Thank you very much for your clear explanation. Yesterday I wrote some code for db relations. It was not so nice to write because I needed to write bunch of almost the same codes.

    I think I have to read more about Doctrine and watch your last 3 tutorial again.
    Thank you.

  4. Aleksandar says:

    Hi John, nice series, and now u have enough material for new casts I think :P

    I just wanted to add small hint for users which are dealing with non latin characters in db tables.

    U can set up doctrine encoding in application.ini by adding doctrine.encoding = “utf8″

  5. Firegun says:

    Hello Guys,

    First of all, I want to congrats you Jon for the excellent work on the casts!

    Then I want to give an idea… I´ve been working on a ZF/Doctrine env, and one thing I´ve noted is that the

    $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);

    Made my app try to load the table classes, witch are something like UserTable for example. After some reading I´ve found that if I add in the application.ini the following line

    doctrine.generate_models_options.generateTableClasses = true

    Doctrine actually generate the table classes… more reading, and a nice thing is, with the table classes u can write what doctrine calls Named Querys, and work whit then in a very nice way, better then adding methods on the model class itself. (I mean, on the last cast we should add the findAll() method on the table class, in reality the findAll method are available on the table class with a nice hydration param).

    And just to say, working with tables are easy, we just need to call

    $oUserTable = Doctrine_Core::getTable(‘User’);

    Anyway, I´m not sure why I have the problems with that bootstrap param, since you haven´t mention nothing on that, but that made me learn more about Doctrine, and I´ve liked the result. And I hope my experience help in some way!

  6. Andre says:

    Thanks for the cast, very useful.

    I’m currently using Propel, the less popular ORM, and I’m probably going to switch down the road to Doctrine.

    I’m wondering though if the performance takes a bit hit with the Doctrine loading of models? What if you had an app with 50+ models? Seems like it would be better to just do include_once where needed?

  7. sNop says:

    Hi Jon,

    I want to thank you for great videos about Doctrine.
    Actually look forward to further videos ;) , i hope that in next videos will be something similar as you post about Zend_Db (one-to-many, many-to-many relations, how to use it with Zend_Form, how to select, update and delete data from tables and so on)

    Thank you

  8. Jay says:

    Great site, I learned a ton from your site, thanks a lot and keep on rocking :)

    I know you have done some casts on consuming web service by leveraging zend_cache and how to zend_gdata. I am wondering if you can do a cast on “how to build a web service with zend framework”?

  9. jon says:

    I think this would be more like a series of videos rather than just one. I’m hoping to re-organize the material on the site in the new year to better accommodate goal-oriented learning, rather than simply highlighting discreet components of the framework. What kind of web service are you trying to build? How big is it? Which protocol are you using? (e.g. SOAP / REST)

  10. Jay says:

    Thanks for your reply Jon. I am considering to build an API for one of my potential projects, I have experience of building web service with asmx and wcf (both Microsoft technologies) and am looking into zend as well as other frameworks, mainly I am looking at performance, scalability, development life time and framework support (in comparison with wcf/asmx).

    It would be fantastic if you can cover both protocols: REST and SOAP, although it is more likely I will first start with REST.

  11. Jay says:

    Would prefer you to cover both protocols (SOAP/REST), I am considering technology platform/language/framework to build a web service with an API, so it will be extremely helpful to me if you can video cast these topics. Some major areas that I am looking at are: performance, scalability, security and learning curve (consider I am an experienced developer).

    Thanks!

  12. It sounds like you’re doing solution evaluation, which, while being something I do for clients, would fall out of the scope of Zendcasts. I’m reluctant to cover SOAP at this point due to some issues related to the Zend Framework code base (however take this personal opinion with a grain of salt since it’s been 6 months since I’ve touched Zend_Soap). also, SOAP is only interesting if it’s truly interoperable with another system, and with the murky waters in which WCF and WebSphere live, I wouldn’t go there without a lot of testing and a lot of cash.

  13. Jay says:

    Understood Jon and thanks for the heads-up. I used WCF for quite a while and seen lots of problem, however, I have seen & heard stories of small start-ups start with PHP and later were forced to rebuild the whole thing in other languages due to its scalability and performance issues (don’t get me wrong, I am a huge fan of PHP). So while I am evaluating the best available solutions, I would to this right in the beginning.

  14. jon says:

    I would venture to guess that the move from PHP to .NET is usually politically driven since there are far more examples of large sites that work with PHP. I’ve also had issues with WCF where I’ve found myself in XML hell. Personally, I don’t buy the SOAP kool-aid. REST is cool and a hell of a lot simpler to implement. I’m not an expert on scalability, but if I had that problem, I’m guessing I’d also have the money to solve it.

  15. dave says:

    think you, it works ! :)
    resolve problems with your first example
    I guess my autoload problem was useful :)

    another question : I have class in library/mylibrary/util/myclass :
    Mylibrary_Util_Myclass
    but with this config it doesn’t find it
    any option I could add to autoload ?

    think’s !

  16. dave says:

    @ Jails :
    “I have seen & heard stories of small start-ups start with PHP and later were forced to rebuild the whole thing in other languages due to its scalability and performance issues”
    interesting, have you stories links ?

    I’m from Java but I found it’s too big for startups
    and big web 2.0 (yahoo) or publications (drupal, wordpress ) are more in php

    I saw Zend has a cach system, I suppos scalabily is a problem for every startup who begin fast and grow.
    and it’s a problem we love ! :)

  17. Andrey says:

    Thank you.

    Can you told about database migration in Doctrine?

  18. Stéphane says:

    Just in case someone run into the same problem:

    $doctrine build-all-reload
    PHP Fatal error: Class ‘BaseCar’ not found in F:\proj\sge\zf-doctrine\application\models\Car.php on line 14

    Just to let you know that I had to change this line in the boostrap.php file:

    Doctrine_Core::loadModels($doctrine['models_path']);
    =>
    Doctrine_Core::loadModels(array($doctrine['models_path'].’/generated’, $doctrine['models_path']));
    (ref: http://stackoverflow.com/questions/1856260/why-cant-i-create-a-doctrine-model-named-album)

    I am working with windows

  19. Sancho says:

    Stéphane, thanks a lot for this tip! You saved my day!

  20. mysticav says:

    Jon, In your video, you only work with rebuilt tables.

    For instance, in the video you create a “Car” Model. Running rebuild-all is quite easy to understand. I also was expecting to see how can you add a table to an existing database without rebuilding everything. Let’s say I have a database and I want to keep its records. So, rebuilding everything is not a choice.

    I’ve been looking for a tutorial about how to update the schema.yml, without rebulding the database (only updating/increment/decrement).

    Is it possible to update the schema.yml, and then run a CLI command that could:
    · Update, Create or delete Model Classes
    · Update, Create or delete tables in database
    · Run without fixtures-loading

    Basically I would love to manage all my database tables from the schema.yml

  21. mysticav says:

    Stéphane, I’m having the exact problem but when run phpunit

    The problem takes place when phpunit tries to generate code coverage report.

    I haven’t found a way to setup autoloading correctly on the testing environment.

    when Doctrine 2.0 arrives, a Zendcast tutorial will be EXTREMELY helpful I hope Jon consider these scenarios:

    - Working with phpUnit
    - Working with existing databases

Leave a Reply

Desktop RSS feed iPhone + iPod