【问题标题】:Accessing database from bootstrap.php in CakePHP在 CakePHP 中从 bootstrap.php 访问数据库
【发布时间】:2009-07-27 03:59:14
【问题描述】:

有没有使用 CakePHP 访问 bootstrap.php 文件中数据库的标准方法?

具体来说,我想将“putenv()”设置为存储在数据库中的时区。是否有另一种方法可以实现我应该使用的相同功能?

谢谢。

【问题讨论】:

  • 我认为 database.php 会更有用。也许不是内置的选项,而是为了组织。

标签: php cakephp bootstrapping


【解决方案1】:

我认为在引导程序中访问数据库不是一个好主意。您不能使用模型,因为它们尚未初始化。我认为您可以使用 PHP 的 mysql_* 提取连接数据并初始化连接并运行查询,但这是一件丑陋的事情。

但是,如果您需要在每次访问您的应用时运行某些操作,我建议将其放在 AppController 构造函数(__construct 函数)中。

class AppController extends Controller {
    public function __construct() {
        // do your magic here

        // call parent constructor
        parent :: __constructor();
    }
}

class YourSpecificController extends AppController {
    public function __construct() {
         // call parent contructor (this) will cause your magic happen
         parent :: __constructor();

         // extra controller initialization instructions
    }
}

如果你没有在扩展类中声明构造函数,你甚至不需要改变任何东西,因为 PHP 会自动调用父 (AppController) 构造函数。

【讨论】:

  • 感谢您的回答。效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 2013-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多