Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Ok so I have made an ORM library for PHP. It uses syntax like so:

*(assume that $business_locations is an array)*

          left_join(BusinessOwner::table(), BusinessOwner::business_id(), SQL::OP_EQUALS, Business::id())->
          left_join(Owner::table(), SQL::OP_EQUALS, Owner::id(), BusinessOwner::owner_id())->
          where(Business::location_id(), SQL::in($business_locations))->

Which can also be represented as:

$query = new Business();
$query->left_join(BusinessOwner::table(), BusinessOwner::business_id(), SQL::OP_EQUALS, $query->id());
$query->left_join(Owner::table(), SQL::OP_EQUALS, Owner::id(), BusinessOwner::owner_id());
$query->where(Business::location_id(), SQL::in($business_locations));

This would produce a query like:

SELECT COUNT(`business_owners`.`id`)
FROM   `businesses`
       LEFT JOIN `business_owners`
         ON `business_owners`.`business_id` = `businesses`.`id`
       LEFT JOIN `owners`
         ON `owners`.`id` = `business_owners`.`owner_id`
WHERE  `businesses`.`type` = 'automotive'
       AND `businesses`.`size` = 'small'
       AND `businesses`.`location_id` IN ( 1, 2, 3, 4 )
GROUP  BY `businesses`.`id`  

Please keep in mind that the syntax might not be prefectly correct (I only wrote this off the top of my head)

  • Any way, what do you think of this style of querying?
  • Is the first method or second better/clearer/cleaner/etc?
  • What would you do to improve it?
share|improve this question
I take it you've tried Doctrine and Propel and found them lacking or two heavy? Maybe read this: – Orbling Feb 6 '11 at 13:48
@orbling Ive never been a fan of annotations. – Petah Feb 6 '11 at 20:31

One small improvement:

return $this 

from each function.

Then you can chain them like this:

share|improve this answer
+1 This is critical. – Orbling Feb 6 '11 at 13:45
It does, and it also returns a new instance from static calls. – Petah Feb 6 '11 at 20:20

I know libraries like mysqli offer both styles. But to me that's slightly odd - there is one important difference between static and OO.

  • 1st style (static) implies you can only do one query at a time.

  • 2nd style (OO) implies you can do more than 1.

So which is true? Personally I'd go with which ever is true.

share|improve this answer

Both styles are horribly messy and verbose. I'd go with something much nicer like Doctrine.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.