开发者

Is it possible to 'prepare' a Propel 1.4 Criteria so it can be used efficiently inside a loop?

I'm using symfony 1.3 with Propel 1.4. I need to use a prepared query that will be used inside a loop ch开发者_高级运维anging the values of bound parameters.

Is it possible to do this with Propel 1.4? I want to retrieve results as objects so don't want to use raw SQL if I can help it.


Propel does not seem to support this out of the box. Take one of the examples, the Book class. BookPeer::doSelect() calls BookPeer::doSelectStmt() and passes the result to BookPeer::populateObjects(). BookPeer::doSelectStmt() calls BasePeer::doSelect(), which always calls BasePeer::createSelectSql() to create the SQL statement, prepares it, and passes it to BasePeer::populateStmtValues() to actually bind the values.

You can take code from these methods to get something like this (without exception or transaction handling):

$criteria = new Criteria();
$criteria->addThis();
$criteria->addThat();


$con = Propel::getConnection($criteria->getDbName(), Propel::CONNECTION_READ);
$params = array();
$sql = BasePeer::createSelectSql($criteria, $params);
$stmt = $con->prepare($sql);
// $stmt is a prepared PDOStatement
// $params is an array of the form: [{'table': 'tableName', 'column': 'columnName', 'value': 'paramValue'}, ...]
// You can keep this format and use BasePeer::populateStmtValues(), or you can call $stmt->bindParam() yourself

// Now in the loop, we set the params ourself
foreach ($loop as $loopData) {
    // Bind the params, using your preferred method
    // ...

    // Pass the prepared and bound statement to populateObjects()
    $books = BookPeer::populateObjects($stmt);

    // Do something with the found objects
    // ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜