如何从模型/控制器访问CodeIgniter配置变量?

时间:2021-11-27 23:08:01

I would like to access the $db['default']['dbprefix'] variable from /application/config/database.php (CodeIgniter configuration file) from within a model so I can write my own queries using the value from the file.

我想从模型中访问/application/config/database.php(CodeIgniter配置文件)中的$ db ['default'] ['dbprefix']变量,这样我就可以使用文件中的值编写自己的查询。

How can this be done?

如何才能做到这一点?

2 个解决方案

#1


22  

Try:

尝试:

$this->load->database();
echo $this->db->dbprefix;

Normally you can use $this->config->item but I think that only allows variables set in $config

通常你可以使用$ this-> config-> item但我认为只允许在$ config中设置变量

#2


7  

The documentation says you should be using:

文档说您应该使用:

$this->db->dbprefix('tablename');

Doesn't make a huge amount of difference but could be an easier syntax.

不会产生巨大的差异,但可能是一种更简单的语法。

#1


22  

Try:

尝试:

$this->load->database();
echo $this->db->dbprefix;

Normally you can use $this->config->item but I think that only allows variables set in $config

通常你可以使用$ this-> config-> item但我认为只允许在$ config中设置变量

#2


7  

The documentation says you should be using:

文档说您应该使用:

$this->db->dbprefix('tablename');

Doesn't make a huge amount of difference but could be an easier syntax.

不会产生巨大的差异,但可能是一种更简单的语法。