CodeIgniter数据库配置+学说 CLI

发布于 2024-11-26 02:00:21 字数 1014 浏览 2 评论 0 原文

我的 config/database.php 文件包含一个简单的条件,可以根据其所在的服务器交换数据库凭据:

$server = $_SERVER['SERVER_NAME'];
if(($server=='localhost')||($server=='127.0.0.1'))
{
    $db['default']['hostname'] = 'MY_LOCAL_IP';
    $db['default']['username'] = 'MY_LOCAL_USERNAME';
    $db['default']['password'] = 'MY_LOCAL_PASSWORD';
    $db['default']['database'] = 'MY_LOCAL_DB';
}
else 
{
    $db['default']['hostname'] = 'MY_PROD_IP';
    $db['default']['username'] = 'MY_PROD_USERNAME';
    $db['default']['password'] = 'MY_PROD_PASSWORD';
    $db['default']['database'] = 'MY_PROD_DB';
}

我开始使用 Doctrine 中,引起我注意的一件事是命令行界面。该提示可以执行各种操作,其中之一是验证条令是否已正确配置以供生产使用。

但是,当在 CLI 中调用 ensure-Production-settings 时,界面对我大喊大叫,指出 SERVER_NAME 是一个未定义的索引。虽然这不是什么问题,但它让我想到 Doctrine 可能在执行我的应用程序期间在后台默默地抱怨同样的事情。

我想我可以制作两个单独的database.php 文件 - 但在此之前 - 有没有人在更改 config/database.php 后遇到过 Doctrine CLI 的问题?

My config/database.php file contains a simple conditional that swaps out database credentials based on the server it lives:

$server = $_SERVER['SERVER_NAME'];
if(($server=='localhost')||($server=='127.0.0.1'))
{
    $db['default']['hostname'] = 'MY_LOCAL_IP';
    $db['default']['username'] = 'MY_LOCAL_USERNAME';
    $db['default']['password'] = 'MY_LOCAL_PASSWORD';
    $db['default']['database'] = 'MY_LOCAL_DB';
}
else 
{
    $db['default']['hostname'] = 'MY_PROD_IP';
    $db['default']['username'] = 'MY_PROD_USERNAME';
    $db['default']['password'] = 'MY_PROD_PASSWORD';
    $db['default']['database'] = 'MY_PROD_DB';
}

I started playing around with Doctrine today and one thing that caught my eye was the Command Line Interface. The prompt can do all sorts of things, one of which is verification that doctrine is properly configured for production use.

But when calling ensure-production-settings in the CLI, the interface yells at me stating that SERVER_NAME is an undefined index. While this isn't much of an issue, it got me thinking that Doctrine might be silently complaining of the same thing in the background during the execution of my application.

I guess I could make two separate database.php files - but before that - has anybody experienced issues with Doctrine CLI after making a change to config/database.php?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空名 2024-12-03 02:00:21

那么 $_SERVER['SERVER_NAME'] 未在 CLI 模式中设置,因为此信息是由网络服务器的 CGI 接口提供的(或者在使用时由网络服务器本身提供) php 作为一个模块)。

为什么不使用 环境 呢?

Well $_SERVER['SERVER_NAME'] is not set in CLI mode, since this information is provided by the CGI-interface of your webserver (or by the webserver itself when using php as a module).

Why don't you use environments for that?

╰◇生如夏花灿烂 2024-12-03 02:00:21

在不破坏您现在所做的事情的情况下,作为执行,您可以为 CLI 定义一个持久值,因为它无法识别某些环境变量...

if(($server=='localhost')||($server=='127.0.0.1'))
{
    $db['default']['hostname'] = 'MY_LOCAL_IP';
    $db['default']['username'] = 'MY_LOCAL_USERNAME';
    $db['default']['password'] = 'MY_LOCAL_PASSWORD';
    $db['default']['database'] = 'MY_LOCAL_DB';
}
else 
{
    $db['default']['hostname'] = 'MY_PROD_IP';
    $db['default']['username'] = 'MY_PROD_USERNAME';
    $db['default']['password'] = 'MY_PROD_PASSWORD';
    $db['default']['database'] = 'MY_PROD_DB';
}

// To catch CLI
if(defined('STDIN'))
{
    $db['default']['hostname'] = 'MY_CLI_IP';
    $db['default']['username'] = 'MY_CLI_USERNAME';
    $db['default']['password'] = 'MY_CLI_PASSWORD';
    $db['default']['database'] = 'MY_CLI_DB';
}

Without breaking what you did right now, as an execption, you could define a persistent values for CLI since it will not recognized some environment variable...

if(($server=='localhost')||($server=='127.0.0.1'))
{
    $db['default']['hostname'] = 'MY_LOCAL_IP';
    $db['default']['username'] = 'MY_LOCAL_USERNAME';
    $db['default']['password'] = 'MY_LOCAL_PASSWORD';
    $db['default']['database'] = 'MY_LOCAL_DB';
}
else 
{
    $db['default']['hostname'] = 'MY_PROD_IP';
    $db['default']['username'] = 'MY_PROD_USERNAME';
    $db['default']['password'] = 'MY_PROD_PASSWORD';
    $db['default']['database'] = 'MY_PROD_DB';
}

// To catch CLI
if(defined('STDIN'))
{
    $db['default']['hostname'] = 'MY_CLI_IP';
    $db['default']['username'] = 'MY_CLI_USERNAME';
    $db['default']['password'] = 'MY_CLI_PASSWORD';
    $db['default']['database'] = 'MY_CLI_DB';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文