$ownerRelation->relateObject($house1, $human1);
$t->is($house1->getOwnerId(), $human1->getId(), 'relateObject() creates a relation between two Propel objects');
$civilityRelation->relateObject($house1, $civility1);
$t->is($human1->getTheSex(), $civility1->getId(), 'relateObject() creates a relation between two Propel objects via another relation');

/******************************************************/
/* sfPropelFinderRelation::relateObject() and aliases */
/******************************************************/

$t->diag('sfPropelFinderRelation::relateObject() and aliases');

HumanPeer::doDeleteAll();
$human1 = new Human();
$human1->setName('John');
$human1->save();
$human2 = new Human();
$human2->setName('Albert');
$human2->save();
$human3 = new Human();
$human3->setName('Jane');
$human3->save();

$manager = new sfPropelFinderRelationManager('Human');
$fatherRelation = $manager->addRelationFromColumns('Human', HumanPeer::FATHER_ID, 'Human', HumanPeer::ID, 'father');
$grandFatherRelation = $manager->addRelationFromColumns('father', HumanPeer::FATHER_ID, 'Human', HumanPeer::ID, 'grandFather');

$fatherRelation->relateObject($human3, $human2);
$t->is($human3->getFatherId(), $human2->getId(), 'relateObject() creates a relation between two Propel objects');
$grandFatherRelation->relateObject($human3, $human1);
$t->is($human2->getFatherId(), $human1->getId(), 'relateObject() creates a relation between two Propel objects via another relation');
$human3->save();

$finder = sfPropelFinder::from('Human')->
  join('Human father', 'Human.FatherId', 'father.Id', 'INNER JOIN')->
  join('Human mother', 'Human.MotherId', 'mother.Id', 'INNER JOIN')->
  where('father.Name', 'John')->
  where('mother.Name', 'Jane');
$nbHumans = $finder->count();
$t->is($nbHumans, 1, 'join() allows to join to the current table with several foreign keys using an alias');
$t->is($finder->getLatestQuery(), propel_sql('SELECT COUNT([P13*][P12human.ID]) FROM human INNER JOIN human father ON (human.FATHER_ID=father.ID) INNER JOIN human mother ON (human.MOTHER_ID=mother.ID) WHERE (father.NAME=\'John\' AND mother.NAME=\'Jane\')'), 'join() uses aliased table names when using an alias relation');

HumanPeer::doDeleteAll();
$human1 = new Human();
$human1->setName('John');
$human1->save();
$human2 = new Human();
$human2->setName('Albert');
$human2->setHumanRelatedByFatherId($human1);
$human2->save();
$human3 = new Human();
$human3->setName('Jane');
$human3->setHumanRelatedByFatherId($human2);
$human3->save();

$finder = sfPropelFinder::from('Human')->
  join('Human father', 'Human.FatherId', 'father.Id', 'INNER JOIN')->
  join('Human grandfather', 'father.FatherId', 'grandfather.Id', 'INNER JOIN')->
  where('grandfather.Name', 'John');
$nbHumans = $finder->count();
$t->is($nbHumans, 1, 'join() allows to join to the current table with several foreign keys using an alias');
$t->is($finder->getLatestQuery(), propel_sql('SELECT COUNT([P13*][P12human.ID]) FROM human INNER JOIN human father ON (human.FATHER_ID=father.ID) INNER JOIN human grandfather ON (father.FATHER_ID=grandfather.ID) WHERE grandfather.NAME=\'John\''), 'join() uses aliased table names when using an alias relation');
$t->is(Propel::getConnection()->getLastExecutedQuery(), $sql, 'with() with multiple foreign keys to the same table avoids subsequent queries');

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

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

HumanPeer::doDeleteAll();
$human1 = new Human();
$human1->setName('John');
$human1->save();
$human2 = new Human();
$human2->setName('Albert');
$human2->setHumanRelatedByFatherId($human1);
$human2->save();

$finder = sfPropelFinder::from('Human')->
  join('Human father', 'Human.FatherId', 'father.Id', 'INNER JOIN')->
  with('father')->
  where('father.Name', 'John');
$human = $finder->findOne();
$sql = 'SELECT human.ID, human.NAME, human.FATHER_ID, human.MOTHER_ID, human.THE_SEX, father.ID, father.NAME, father.FATHER_ID, father.MOTHER_ID, father.THE_SEX FROM human INNER JOIN human father ON (human.FATHER_ID=father.ID) WHERE father.NAME=\'John\' LIMIT 1';
$t->is($finder->getLatestQuery(), $sql, 'with() adds the correct columns to the query when called with self-referenced foreign keys');
$t->is($human->getName(), 'Albert', 'with() with self-referenced foreign keys does not change the main object hyration');
$t->is($human->getHumanRelatedByFatherId()->getName(), 'John', 'with() with self-referenced foreign keys hydrates the related objects');
$t->is(Propel::getConnection()->getLastExecutedQuery(), $sql, 'with() with self-referenced foreign keys avoids subsequent queries');

/******************************/
/* sfPropelFinder::withI18n() */
/******************************/