我有大型MySQL数据库。我想一次获取100行,直到所有数据库都被处理完毕。
protected function doEcho($msg){
static $time, $start;
if (!$start) $start = time();
if (time() - $time > 0) {
echo $msg . "\n\n";
$time = time();
}
}
// ZF2 action
public function stuffAction() {
$request = $this->getRequest();
if (!$request instanceof ConsoleRequest){
throw new \RuntimeException('You can only use this action from a console!');
}
// Propel ORM generated Model UserQuery
$q = \UserQuery::create()->limit(100)->filterByProcessed(0);
$users = $q->find();
while ($users->count() ) {
foreach ($users as $user) {
$id = $user->getId();
$email = $user->getEmail();
$password = $user->getPassword();
$this->doEcho("$id - $email - $password");
// do stuff and set processed to 1
}
$q = \UserQuery::create()->limit(100)->filterByProcessed(0);
$users = $q->find();
}
}
我收到以下异常:
=============================================== =======================
行走\运行\异常\ PropelException
user
WHERE user.PROCESSED =:p1限制100] // debug backtrace
=============================================== =======================
PDOException
// debug backtrace
显然,原因是:
An application con正在准备数据库正在准备sql 语句,执行它们,然后不关闭它们。
如何让Propel结束发言?我确实使用了Propel 2:"推进/推进":" 2.0。* @ dev"
答案 0 :(得分:0)
解决:
use Propel\Runtime\Propel;
use Propel\Runtime\Connection\ConnectionWrapper;
// ...
Propel::getServiceContainer()
// connection_name - connection from propel config
->getReadConnection('connection_name')
->setAttribute(ConnectionWrapper::PROPEL_ATTR_CACHE_PREPARES, true);