是否有可能“准备”Propel 1.4标准,以便在循环内有效使用?

时间:2010-09-10 09:08:01

标签: prepared-statement propel

我正在使用带有Propel 1.4的symfony 1.3。我需要使用一个准备好的查询,它将在循环中使用,改变绑定参数的值。

是否可以使用Propel 1.4执行此操作?我想将结果检索为对象,所以如果我能帮助它,就不要使用原始SQL。

1 个答案:

答案 0 :(得分:2)

Propel似乎不支持开箱即用。以其中一个示例为Book类。 BookPeer::doSelect()调用BookPeer::doSelectStmt()并将结果传递给BookPeer::populateObjects()BookPeer::doSelectStmt()调用BasePeer::doSelect(),它始终调用BasePeer::createSelectSql()来创建SQL语句,准备它,并将其传递给BasePeer::populateStmtValues()以实际绑定值。

您可以从这些方法中获取代码以获得类似的代码(无例外或事务处理):

$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
    // ...
}