I am using custom SQL to join two tables, apply some business logic to dates, then use the results to hydrate a propel object (collection). Here's my code:
$testtypes = TesttypeQuery::create()->find();
foreach ($testtypes as $testtype) {
  /* work out what most recent schedule */
  $con = \Propel::getConnection(SchedulePeer::DATABASE_NAME);
  $sql = "SELECT `schedule`.*, (`schedule`.`last` + INTERVAL `duration`.`weeks` WEEK + INTERVAL `duration`.`months` MONTH + INTERVAL `duration`.`years` YEAR) AS `dueDate` FROM `schedule` LEFT JOIN `duration` ON `schedule`.`duration_id` = `duration`.`id` HAVING `schedule`.`testtype_id` = {$testtype->getId()} AND `dueDate` < NOW() ORDER BY `dueDate` ASC LIMIT 1";
  $stmt = $con->prepare($sql);
  $stmt->execute();
  $formatter = new \PropelObjectFormatter();
  $formatter->setClass(SchedulePeer::OM_CLASS);
  $schedules = $formatter->format($stmt);
  // more stuff here ... 
}
This question comes in several parts, because there might be a completely better way of doing this - so please feel free to make suggestions other than just answering my specific questions:
HAVING instead of WHERE so that I can use the aliased column dueDate, which I want to use as part of the check and order, as well as returning it as part of the result-set to use later. Is there a way to grab this value but still hydrate the propel object? When I use fetch() or other PDO methods on $stmt I can no longer use this with the call to format().You should add some steps. This is the logical process:
Hope this helps. Cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With