recent work with zend framework and doctrine2

I recently began working with an organization and we are in the midst of making some changes. They were not far enough along that any changes now make that much of a difference, but it was an odd set of changes nonetheless.

Initially, the client had started with CodeIgniter (during a phone call earlier, there was interest in ZF2, but when I started they’d gone with CI). One of the first things I tried to do was to get unit testing set up with CI, and the CIUnit failed out of the box. We’re using PHP5.3, and apparently CIUnit doesn’t work well with PHP5.3. I was on the fence about whether we should try to use a vanilla phpunit setup, but I counseled against it (was that wise?). CI was not hurting anyone, but it’s also PHP4 compatible (still), which strikes me as a drag. I can’t quite put my finger on it, and I know that ‘our’ code can still take advantage of PHP5 stuff. However, with things like split() being deprecated in 5.3, I think we might start seeing more warnings if/when we started exercising more of CI. I might be wholly wrong though. I had a bad experience (multiple) with Kohana, so don’t go there. :)

I’ve had zfkit in place for a while now, and suggested we look at that. However, zfkit has doctrine1 integrated, and the code the client had was already built around doctrine2. That took a little bit of cajoling to get working – once I’m a bit more comfortable with it, I plan to roll in doctrine2 support in to zfkit. I was a bit surprised to see some of the more useful aspects of doctrine1 were removed – the ‘acts as timestampable’, for example. The authors disagreed that something like that is fundamental, and offer some ways around it, but those workarounds are fairly inelegant, imo. The more I work with doctrine2, the clumsier this sort of stuff feels in PHP. Yes, I know, the language itself is the impediment, and that’s why many of my recent projects have been in Grails instead of PHP. For basic work, GORM is a lifesaver compared to doctrine and other PHP ORM tools. That said, phpunit is miles faster than unit testing in Grails, so there’s always tradeoffs. Also, this client has chosen to do their doctrine definitions in php docblock annotations. They irk me. :(

Still, we’ll have managed to port over the base of the existing app, have a replicatable(?) data story (drop/update schema, load test data) and the start of unit tests (possibly automated later with phpundercontrol) in less than a week’s time. This doesn’t feel too bad, all in all.

I did run in to a ‘problem’ (well, for me anyway) in that the client’s Doctrine models are namespaced. Referencing PHP’s DateTime object from inside a namespaced class requires it to be referenced as \DateTime().

namespace foo;
class bar {
  public function demo() {
    $now = new \DateTime("now");

That was a bit new to me, and I didn’t see any specific references to that online, but I may have missed it somewhere. Would have been nice for PHP to drop up to a global scope, or understand references to its own built-in objects, rather than having to do this workaround.

Note: for some reason, an earlier posted tagged ‘business’ made it in to planet-php’s feed. It should only be pulling ‘PHP’ tags. Apologies if it seemed I was spamming the aggregator.

I'm currently working on a book for web freelancers, covering everything you need to know to get started or just get better. Want to stay updated? Sign up for my mailing list to get updates when the book is ready to be released!

Web Developer Freelancing Handbook

Be Sociable, Share!

{ 9 comments to read ... please submit one more! }

  1. What you experienced with DateTime is actually “a good thing.” Basically, when within a namespaces, any reference to a class is assumed to be a relative to the current namespaces. This allows you to override PHP classes with your own extensions and/or implementations. (we’re doing this in zf2 to make several of the SPL datastructure classes serializable.)

    For consistency, then, any time you reference a built-in class, you then need to qualify it with the leading namespace separator, which tells PHP to start looking not in the current namespace, nut from the global one.

    Originally, functions were going to operate in this way as well. However, usage of functions happens literally everywhere, and it was decided that by default, unless a definition existed in the current namespace, they’d use the global definition. I find this to be inconsistent, but pragmatic – much like PHP in general. ;-)

  2. I *get it* (or ‘got it’) the other day, it’s just not what I was expecting. Given the ‘pragmatic’ nature you, I would have assumed it would look first in the namespace, then for global. Prefixing with \ would bypass the local lookup, but I still was expecting a fallback. Oh well…

  3. FWIW, I did see that this is documented (I hadn’t found it before) at

    “Inside a namespace, when PHP encounters a unqualified Name in a class name, function or constant context, it resolves these with different priorities. Class names always resolve to the current namespace name. Thus to access internal or non-namespaced user classes, One must refer to them with their fully qualified Name”

  4. I’ve forked your project and updated the doctrine bootstrap to use the code I use for all my projects. It’s different from yours in that it takes into account caching and enabling DQL callbacks and validators.

    I also structure my models so that they are autoloaded by ZF. Hope you find these mods useful.

  5. Cool! You’re still using doctrine1 for this? If so, have you tried it with doctrine2 yet?

  6. sorry for offtop, but i think that your social bookmarks should be inline :)

  7. this was some known bug with sociable and some upgrade – should be fixed now. thanks.

  8. Your readme states doctrine 1 not 2?

    What are your thoughts on this project?–Doctrine-ORM–PHPUnit–Ant–Jenkins-CI–TDD-

    Would it be worth you & eddie merging projects? You both use latest ZF & latest Doctrine

  9. It might be – I’m going to look at his project next month. It may be further along than I am (I think so).

{ 0 Pingbacks/Trackbacks }

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>