您可以使用自己的 SQL 执行查询,但没有自动方式将 sql 转换为 Criteria 对象。
$con = Propel::getConnection(DATABASE_NAME);
$sql = "SELECT books.* FROM books
WHERE NOT EXISTS (SELECT id FROM review WHERE book_id = book.id)";
$stmt = $con->createStatement();
$rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM);
$books = BookPeer::populateObjects($rs);
这会一起绕过 Criterion 对象。您提到想要一个标准对象,以便您可以将其输入寻呼机。您可以改为在寻呼机中设置自定义选择方法,然后该寻呼机将执行您的自定义查询。如果您需要向其中传递参数,我建议使用您自己的寻呼机类扩展 sfPropel,该类可以选择将参数传递给您的对等选择方法,因此您根本不必使用 Criteria 对象。作为一种快速的替代方法,您可以执行以下操作,将您的 Criteria 用作您选择参数的容器:
$c = new Criteria();
$c->add(DiscussreplyPeer::ID, $myId);
$pager = new sfPropelPager();
$pager->setCriteria($c);
$pager->setPeerMethod('getReplies');
然后在你的同龄人中:
public static function getReplies(Criteria $c) {
$map = $c->getMap();
$replyId = $map[DiscussreplyPeer::ID]->getValue();
$con = Propel::getConnection(DATABASE_NAME);
$sql = "select p.disrepid, p.subject, p.body, c.disrepid as disrepid1, c.subject as subject1, c.body as body1 from discusreply as p, discusreply as c where p.distopid=? and (c.disrepid = p.parentid or c.parentid = p.distopid) order by p.disrepid ASC";
$stmt = $con->prepareStatement($sql);
$stmt->setString(1, $replyId);
$rs = $stmt->executeQuery();
$results = array();
while ($rs->next()) {
// for example
$results['disrepid'] = $rs->getInt('disrepid');
}
return $results;
}
有关推进和 symfony 的更多提示可以在以下位置找到:
http ://stereointeractive.com/blog/2007/06/12/propel-queries-using-custom-sql-peer-classes-and-criterion-objects/