SUBSCRIBE VIA RSS


Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.

Topics

Stack Overflow


The old fashioned way

RECENT TUNES

January 24, 2008 – 4:48pm Propel Criteria Left Join: using addJoin() and addAlias() to join a table twice

Here is a way to do left join on two columns in a table that both were foreign keys in the same table using Propel. This might happen if for example you have a table with a parent_id and a child_id in a nested set that both refer to the same table, or as an example you have a table “documentation” and each documentation can be categorized into a main category and a sub category.

What happens when you want to do a left join in your documentation query, so that if present both the related category and sub_category objects get included in the results?

$c->addJoin(DocumentationPeer::CATEGORY, CategoryPeer::ID, Criteria::LEFT_JOIN);
$c->addJoin(DocumentationPeer::SUB_CATEGORY, CategoryPeer::alias('c2', CategoryPeer::ID), Criteria::LEFT_JOIN);
$c->addAlias('c2', CategoryPeer::TABLE_NAME);

Done and done!

Posted by in  Web Development   |  

4 Responses to Propel Criteria Left Join: using addJoin() and addAlias() to join a table twice

  1. Pingback: rpsblog.com » A week of symfony #56 (21->27 January 2008)

  2. hartym says:

    hi

    by any hasard, did you find a way to join the same table twice using the same foreign key? that may seems strange but there are case where that could be usefull, for example in nested set implementations (retrieving category breadcrumb) or in many to many relation (A)-(B) in chich you want to retrieve every records A which are related to same B records than a model A.

    I tryed hard to find a way to do this but I had no chance… Hope you can enlighten me on this 🙂

    Hartym

  3. Pingback: Symfony.es » Blog Archive » Una semana con Symfony #29 (21->27 enero 2008)

  4. Leon says:

    Thanks,

    This brings me again a little closer to my first demo!