Skip to content

Instantly share code, notes, and snippets.

@markbalt
Created May 30, 2012 16:04
Show Gist options
  • Save markbalt/2837240 to your computer and use it in GitHub Desktop.
Save markbalt/2837240 to your computer and use it in GitHub Desktop.
Propel Criteria: Multiple LEFT JOIN with Alias WHERE NULL
$criteria->addAlias("acc", AccountPeer::TABLE_NAME);
$criteria->addMultipleJoin(array(
array(CustomerPeer::ID, AccountPeer::alias("acc", AccountPeer::CUSTOMER_ID)),
array(AccountPeer::alias("acc", AccountPeer::BILLING_SYSTEM_ID), BillingSystemPeer::PORTAL_65),
array(AccountPeer::alias("acc", AccountPeer::IS_ACTIVE), 1)),
Criteria::LEFT_JOIN);
$criteria->add(AccountPeer::alias("acc", AccountPeer::ID), null, Criteria::ISNULL);
$criteria->setDistinct(1);
@bishopb
Copy link

bishopb commented Jul 8, 2015

Thanks for the gist. Sadly, Propel (in this version) is wearisomely verbose, 😫

    $uo = 'uo';
    $os = 'os';
    $o  = 'o';

    $criteria->addAlias($uo, PropelUserOrgPeer::TABLE_NAME);
    $criteria->addJoin(
      PropelUserPeer::ID,
      PropelUserOrgPeer::alias($uo, PropelUserOrgPeer::USER_ID),
      PropelCriteria::LEFT_JOIN
    );

    $criteria->addAlias($os, PropelOrgSetPeer::TABLE_NAME);
    $criteria->addJoin(
      PropelUserOrgPeer::alias($uo, PropelUserOrgPeer::ID),
      PropelOrgSetPeer::alias($os, PropelOrgSetPeer::TARGET_USER_ORG_ID),
      PropelCriteria::LEFT_JOIN
    );

    $criteria->addAlias($o, PropelOrgPeer::TABLE_NAME);
    $criteria->addJoin(
      PropelOrgSetPeer::alias($os, PropelOrgSetPeer::ORG_ID),
      PropelOrgPeer::alias($o, PropelOrgPeer::ID),
      PropelCriteria::LEFT_JOIN
    );

to accomplish:

   LEFT JOIN `user_org` `uo` ON (user.id=uo.user_id)
   LEFT JOIN `org_set` `os` ON (uo.id=os.target_user_org_id)
   LEFT JOIN `org` `o` ON (os.org_id=o.id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment