代码之家  ›  专栏  ›  技术社区  ›  timmy

是否可以“准备”推进1.4标准,以便在回路内有效使用?

  •  2
  • timmy  · 技术社区  · 14 年前

    我用的是symfony 1.3和推进1.4。我需要使用一个准备好的查询,该查询将在更改绑定参数值的循环中使用。

    推进1.4可以这样做吗?我想将结果作为对象来检索,所以如果我能提供帮助,就不想使用原始SQL。

    1 回复  |  直到 14 年前
        1
  •  2
  •   Jan Fabry    14 年前

    推进似乎不支持这个开箱即用。举一个例子, 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
        // ...
    }