如何访问库类中的数据库? [英] How to access database in library class?

查看:117
本文介绍了如何访问库类中的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重写我的应用程序以使用CodeIgniter。目前我正在努力与DB。我在 config / databases.php 中配置了数据库,但不知道如何在库级别访问它们。


通过 autoload.php 加载库:

  $ autoload ['libraries' ] = array('database','FGMembersite','phpmailer','formvalidator'); 

FGMembersite是我用于注册和登录的类。

我在 login.php 视图中访问此类:

  if(isset($ _ POST ['submitted']))
{
if($ this-> fgmembersite-> Login())
{
$ this-> ; fgmembersite-> RedirectToURL(main);
}
}

fgmembersite->登录()导致:

  public function DBLogin()
{

$ this-> connection = pg_connect(host = localhost port = 5432 dbname =。$ this-> db-> database。user =。$ this-> ci-> db - > username。password =。$ this-> db-> password。);

DBLogin()理解 db 是什么。我收到的错误如

未定义的属性:FGMembersite :: $ db


尝试获取非对象的属性


我应该如何访问我的 FGMembersite 中的数据库配置?

解决方案

您需要使用 CodeIgniter超级对象,以从其他库中访问自动加载的库。像这样

  $ ci =& get_instance(); 

$ this-> connection = pg_connect(host = localhost port = 5432 dbname =。$ ci-> db-> database。
user =。$ ci - > db-> username。
password =。$ ci-> db-> password);

看起来你试图在代码中使用这个方法, username 字段,而不是数据库密码 / p>

您可以通过将ci实例分配给类构造函数中的类属性来保存它,如下所示:

  class Yourclass {

private $ ci;

public function __construct(){

$ this-> ci =& get_instance();

}

public DBLogin(){

$ this-> connection = pg_connect(host = localhost port = 5432 dbname =。
$ this-> ci-> db-> database。
user =。$ this-> ci-> db-> username。
password = 。$ this-> ci-> db-> password);
}

public somethingElse(){

print_r($ this-> ci-> db-> username);

}

}


I'm rewriting my application to make use of CodeIgniter. Currently I'm struggling with DB. I've configured databases in config/databases.php but have no idea how to access them at a library level.

I'm loading libraries via autoload.php:

$autoload['libraries'] = array('database', 'FGMembersite', 'phpmailer', 'formvalidator');

FGMembersite is a class which I'm using for registration and login.
I access this class in login.php view where I have:

if(isset($_POST['submitted']))
{
   if($this->fgmembersite->Login())
   {
        $this->fgmembersite->RedirectToURL("main");
   }
}

fgmembersite->Login() leads to:

public function DBLogin()
{

  $this->connection = pg_connect("host=localhost port=5432 dbname=".$this->db->database." user=".$this->ci->db->username." password=".$this->db->password."");

but DBLogin() here doesn't understand what is db . I receive errors like
Undefined property: FGMembersite::$db
or
Trying to get property of non-object

How I'm supposed to access database config in my FGMembersite?

解决方案

You need to use the CodeIgniter super object to gain access to the auto loaded libraries, from within other libraries. Like so

$ci =& get_instance();

$this->connection = pg_connect("host=localhost port=5432 dbname=" . $ci->db->database . 
                               " user=" . $ci->db->username . 
                               " password=" . $ci->db->password );

It looks like you were trying to use this method in your code, except you only used it for the username field, not the database and password ones.

You can save the ci instance by assigning it to a class property in your classes constructor, like so:

class Yourclass {

    private $ci;

    public function __construct() {

        $this->ci =& get_instance();  

    }

    public DBLogin() {

         $this->connection = pg_connect("host=localhost port=5432 dbname=" .
                             $this->ci->db->database . 
                               " user=" . $this->ci->db->username . 
                               " password=" . $this->ci->db->password );
    }

    public somethingElse() {

        print_r ($this->ci->db->username);

    }

}

这篇关于如何访问库类中的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆