我试图在游标迭代中获取一些结果,所以我得到相关的元素和东西。但是,当在迭代中执行此查询时,迭代游标死亡,迭代将结束,即使有更多的数据要获取。
一个例子protected $em;
public function __construct() {
$this->em = $this->get('doctrine')->getManager('default');
}
public function getProducts() {
$query = "SELECT * FROM products"; // 100 Products
$stmt = $this->select($query);
while($result = $stmt->fetch()) {
// This breaks the cursor so only 1 product is parsed.
// If I remove this line, iteration ends perfectly (100 products)
$pictures = $this->getPictures($result['id']);
}
}
public function getPictures($product) {
$query = "SELECT * FROM pictures WHERE product_id = :product_id";
$stmt = $this->select($query, array('product_id' => $product));
return $stmt->fetchAll();
}
public function select($query, array $params = array()) {
$conn = $this->em->getConnection();
$stmt = $conn->prepare($query);
foreach($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$stmt->execute();
return $stmt;
}
我认为如果不关闭前一个光标,就不能使用另一个光标。如果您查看ResullStatement接口:
/**
* Closes the cursor, enabling the statement to be executed again.
*
* @return boolean TRUE on success or FALSE on failure.
*/
public function closeCursor();
当游标关闭时,通常会释放stmt内存。那么,为什么不为此编写一个查询呢?您可以使用子查询提取所有产品id。比如: