2014-05-23 80 views
2

想不通這一個:什麼時候CodeIgniter中的「建立連接」應該是真的?

DB_driver.php(核心系統/數據庫)

這是開始初始化函數的...

/** 
    * 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()被稱爲wihtin CI_Controller所以當創建新的對象,例如像這樣:

$x = new PartyPooper(); 
$y = new PartyPooper(); 

initialize()DB_driver被調用兩次。沒有什麼奇怪的,但我期望在創建PartyPooper()對象第二次($y)時設置$this->conn_id

什麼時候「建立連接」應該是真的? (在這個例子中兩個數據庫連接是由什麼時候就應該只有一個?)

我使用的開發分支最新的數據庫驅動程序:https://github.com/EllisLab/CodeIgniter/tree/develop/system/database

我使用的mysqli驅動器合併持續連接的。

UPDATE:

我不是一個巨大的風扇與核心文件搞亂,但我不能在這裏找出另一種解決方案。請告訴我是否有更好的方法來實現我想要做的事情。

我想出了這個代碼(使用會話來處理存儲和現有數據庫的連接檢查(以下簡稱「子驅動器」 -object的(在我的情況的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

但我原來的問題依然存在:?

當這個原本應該在initialize()功能真正

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

(改變代碼的時候,因爲我想它,我沒有刪除它有一些目的,但是即使我不能找出哪些)

UPDATE2 - 澄清:

在模型中,我有一個應該返回PartyPooper對象的db select語句:

例如。 $res = $q->result('PartyPooper');

此PartyPooper是一個控制器,存儲有關人的信息,如姓名,年份,生日等,但它也處理像在同一個對象內計算信息的東西。

class PartyPooper extends CI_Controller { .... } 

但據我所知,從下面的評論我應該這樣做呢?

class PartyPooper extends CI_Controller { .... } 
class PartyPooperObject { .... } //Store information about people in this object 

例如, $res = $q->result('PartyPooperObject');

+0

爲什麼你的'PartyPooper'類實際調用**控制器**類的初始值設定項?控制器是處理請求的東西。它們不是要模擬「正常」的對象。如果你的類定義是'class PartyPooper extends CI_Controller',你應該改變它。否則,請添加該行,以便我們可以看到您的類和其餘CodeIgniter之間的關係。 – GhostGambler

+0

是的,它擴展到CI_Controller。在PartyPooper類中,它處理對PartyPooper模型類(處理數據庫請求)的請求。我應該更改什麼擴展CI_Controller? – bestprogrammerintheworld

+0

那麼,如果它是一個控制器,它應該擴展'CI_Controller'類。但是每個請求應該只有一個控制器對象...爲什麼你有多個控制器對象?通常,路由代碼會創建一個控制器對象,它將模型和視圖粘合在一起並返回響應。所以一個請求,一個控制器對象。 – GhostGambler

回答

1

這段代碼

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

只是確保了initialize方法僅稱爲一次每個對象

如果已經在當前對象上調用過它,則不能再次執行它。

+0

非常感謝!這回答我實際問題,但感謝您的其他幫助,讓我走上正軌。當我不得不侵入核心文件時,我認爲有什麼錯誤... – bestprogrammerintheworld

相關問題