我使用 Propel 连接和查询数据库,从 symfony 2.7 升级到 symfony 2.8 后,我遇到了奇怪的问题。当我完全删除缓存目录的内容然后运行app/console
命令时,我得到了这个异常:
[PropelException] 数据源的运行时配置文件中没有连接信息 [默认]
每次我更改配置或翻译文件然后刷新页面时也会发生这种情况。当我再次这样做时,它工作正常。
我发现这是由我的一项服务引起的,该服务作为全局变量传递给 twig,如下所示app/config/config.yml
:
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
globals:
settings: @app.settings
该服务尝试在构造函数中从数据库加载数据:
<?php
namespace AppBundle\Services;
use AppBundle\Propel\SettingsQuery;
class PropelSettings {
private $settings = array();
public function __construct() {
$this->initialize();
}
public function initialize() {
$settings = SettingsQuery::create()
->select(array('key', 'value'))
->find();
foreach ( $settings as $s ) {
$this->settigns[$s['key']] = $s['value'];
}
}
public function get($key, $default = null) {
return isset($this->settings[$key]) ? $this->settings[$key] : $default;
}
}
但这发生在通过调用 boot() 方法来推动加载配置之前vendor/propel/propel-bundle/Propel/PropelBundle/PropelBundle.php
,这意味着 symfony 会在预热期间实际加载所有包之前尝试创建服务实例。这在 symfony 2.7 和更早的版本中不是问题。这可以通过在构建服务期间不查询数据库来轻松避免,但是 symfony 2.8 的这种行为是否正确?在 symfony 创建任何服务实例之前,有什么方法可以强制启动推进器?