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

TWITTER

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 in  Web Development   |     |  delicious  Digg

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

  1. 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

    By hartym on Jan 28, 2008

  2. Thanks,

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

    By Leon on Jul 1, 2008

2 Trackback(s)

  1. Jan 27, 2008: rpsblog.com » A week of symfony #56 (21->27 January 2008)
  2. Feb 3, 2008: Symfony.es » Blog Archive » Una semana con Symfony #29 (21->27 enero 2008)

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word