在codeigniter中动态设置我的配置变量后如何从其他控制器和模型访问它们?

在codeigniter中动态设置我的配置变量后如何从其他控制器和模型访问它们?

问题描述:

I have updated my config variable in my model using:

$this->config->set_item('userid', $user_id);

if i echo it in the model i can see that it gets set.

But if i echo it in the controller or another model using:

echo $this->config->item('userid');

it shows the original value.

I need to store this config variable throughout the session but i do not want to use session variables.

我在模型中更新了我的配置变量: p>

  $ this-> config-> set_item('userid',$ user_id); 
  code>  pre> 
 
 

如果我在模型中回显它,我可以看到它得到它 但是如果我在控制器或其他模型中使用以下方式回显: p>

  echo $ this-> config->  item('userid'); 
  code>  pre> 
 
 

它显示原始值。 p>

我需要将此配置变量存储在整个 会话,但我不想使用会话变量。 p> div>

that configurtation is only applicable on that model which you set it. If you want a global setting without using session.

you could create a core model on application/core and name it

MY_Model

so basically what MY_Model do is that it sets your config on all models that extends it.

class MY_Model Extends CI_Model
{
  protected $user_id;
  public function __construct()
  {
    parent::__construct();
   $this->config->set_item('userid', $this->user_id);
  }
}

then on your model that you want the settings to be applied, just extends your model. like

Model extends MY_Model
{

  public function test($id)
  {
    $this->user_id = $id;

   }
}

OR you could create a Core COntroller same as the above but substituting controller to model read more at http://ellislab.com/codeigniter/user-guide/general/core_classes.html

Try to initialize the config $this->ci = & get_instance(); and then set the value like $this->ci->config->set_item('userid', $user_id);

I have not tested this... but I assume it will work.