HousePeer::doDeleteAll();
$human1 = new Human();
$human1->setName('John');
$human1->save();
$human2 = new Human();
$human2->setName('Jane');
$human2->save();
$house1 = new House();
$house1->setName('Home1');
$house1->setHumanRelatedByOwnerId($human1);
$house1->setHumanRelatedByRenterId($human2);
$house1->save();
$house2 = new House();
$house2->setName('Home2');
$house2->setHumanRelatedByOwnerId($human2);
$house2->setHumanRelatedByRenterId($human1);
$house2->save();
$finder = sfPropelFinder::from('House')->
  join('Human owner', 'House.OwnerId', 'owner.Id', 'INNER JOIN')->
  where('owner.Name', 'John');
$nbHouses = $finder->count();
$t->is($nbHouses, 1, 'join() allows to join to another table with several foreign keys using an alias');
$t->is($finder->getLatestQuery(), propel_sql('SELECT COUNT([P13*][P12house.ID]) FROM house INNER JOIN human owner ON (house.OWNER_ID=owner.ID) WHERE owner.NAME=\'John\''), 'join() uses aliased table names when using an alias relation');

/************************************************************/
/* sfPropelFinder::join() with self-referenced foreign keys */
/************************************************************/

$t->diag('sfPropelFinder::join() with self-referenced foreign keys');

HumanPeer::doDeleteAll();
/***********************************************************************/

$t->diag('sfPropelFinder::with() with multiple foreign keys to the same table');

HumanPeer::doDeleteAll();
HousePeer::doDeleteAll();
$human1 = new Human();
$human1->setName('John');
$human1->save();
$human2 = new Human();
$human2->setName('Jane');
$human2->save();
$house1 = new House();
$house1->setName('Home1');
$house1->setHumanRelatedByOwnerId($human1);
$house1->setHumanRelatedByRenterId($human2);
$house1->save();
$finder = sfPropelFinder::from('House')->
  join('Human owner', 'House.OwnerId', 'owner.Id', 'INNER JOIN')->
  with('owner')->
  where('owner.Name', 'John');
$house = $finder->findOne();
$sql = 'SELECT house.ID, house.NAME, house.OWNER_ID, house.RENTER_ID, owner.ID, owner.NAME, owner.FATHER_ID, owner.MOTHER_ID, owner.THE_SEX FROM house INNER JOIN human owner ON (house.OWNER_ID=owner.ID) WHERE owner.NAME=\'John\' LIMIT 1';
$t->is($finder->getLatestQuery(), $sql, 'with() adds the correct columns to the query when called on a foreign key to a table with more than one relation');
$t->is($house->getName(), 'Home1', 'with() with multiple foreign keys to the same table does not change the main object hyration');
$t->is($house->getHumanRelatedByOwnerId()->getName(), 'John', 'with() with multiple foreign keys to the same table hydrates the related objects');
$t->is(Propel::getConnection()->getLastExecutedQuery(), $sql, 'with() with multiple foreign keys to the same table avoids subsequent queries');

$finder = sfPropelFinder::from('House')->
  join('Human owner', 'House.OwnerId', 'owner.Id', 'INNER JOIN')->
  join('Human renter', 'House.RenterId', 'renter.Id', 'INNER JOIN')->