独自の 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 オブジェクトをまったく使用する必要がなくなります。簡単な代替手段として、選択したパラメーターのコンテナーとして基準を使用して、次のようなことを行うことができます。
$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;
}
propel と symfony に関するその他のヒントは、
http ://stereointeractive.com/blog/2007/06/12/propel-queries-using-custom-sql-peer-classes-and-criterion-objects/ にあります。