我想不通:
DB_driver.php(核心系统/数据库)中
这是init函数的开始…

    /**
     * Initialize Database Settings
     *
     * @return  bool
     */
    public function initialize()
    {
        /* If an established connection is available, then there's
         * no need to connect and select the database.
         *
         * Depending on the database driver, conn_id can be either
         * boolean TRUE, a resource or an object.
         */
        if ($this->conn_id)
        {
            return TRUE;
        }

        // ----------------------------------------------------------------

        // Connect to the database and set the connection ID
        $this->conn_id = $this->db_connect($this->pconnect);

initialize()称为wihtinCI_Controller,因此在创建新对象时,例如:
$x = new PartyPooper();
$y = new PartyPooper();

initialize()中的DB_driver调用两次。没什么奇怪的,但是我希望在第二次创建对象时设置$this->conn_id
什么时候“建立的联系”应该是真的?(在这个例子中,当只有一个数据库连接时,会建立两个数据库连接?)
我在开发部门使用最新的数据库驱动程序:https://github.com/EllisLab/CodeIgniter/tree/develop/system/database
我使用的mysqli驱动程序具有的持久连接。
更新:
我不是一个热衷于处理核心文件的人,但我在这里找不到其他解决方案。请告诉我是否有更好的方法来实现我想做的事情。
我提出了这个代码(使用会话来处理存储和检查现有的DB连接(子驱动)-对象。(在我的例子中是mysqli对象):
public function initialize()
{
        /* If an established connection is available, then there's
         * no need to connect and select the database.
         *
         * Depending on the database driver, conn_id can be either
         * boolean TRUE, a resource or an object.
         */
        if ($this->conn_id) {
                return TRUE;
        }

        $conn_session_id_name = 'dbsession_conn';
        if (isset($_SESSION[$conn_session_id_name])) {
                $sess = $_SESSION[$conn_session_id_name];

                // Set connection id object or resourse and return true
                // because no more connecting has to be done
                if (is_object($sess) || is_resource($sess)) {
                        $this->conn_id = $sess;
                        return TRUE;
                }
        }
            // Connect to the database and set the connection ID
        $this->conn_id = $this->db_connect($this->pconnect);

        // Store conn object or resource etc into session
        if (is_object($this->conn_id) || is_resource($this->conn_id)) {
                $_SESSION[$conn_session_id_name] = $this->conn_id;
        }

        // No connection resource? Check if there is a failover else throw an error
            if ( ! $this->conn_id)
            {
            //rest of code as before...

我的应用程序变得非常快,因为它使用了一个连接而不是大约60个。
但我最初的问题是:
什么时候在PartyPooper()函数中应该是这样的?
if ($this->conn_id) {
    return TRUE;
}

(我在修改代码时没有删除它,因为我认为它有一些用途,但即使我不知道是哪个)
更新2-澄清:
在模型中,我有一个db select语句,它应该返回partypooper对象:
例如$y
partypooper是一个控制器,它存储关于人的信息,如姓名、年份、生日等,但它也处理同一对象中的信息计算等。
class PartyPooper extends CI_Controller { .... }

但据我从下面的评论中了解,我应该这样做,而不是?
class PartyPooper extends CI_Controller { .... }
class PartyPooperObject { .... } //Store information about people in this object

例如initialize()

最佳答案

这段代码

    if ($this->conn_id) {
            return TRUE;
    }

只需确保每个对象只调用一次initialize方法。
如果已经在当前对象上调用了它,则不能再执行它。

关于php - 在CodeIgniter中什么时候应该建立“已建立的连接”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23839310/

10-14 18:52
查看更多