2013-11-27 52 views
3

我是新來的CodeIgniter並試圖發展與它相當簡單的應用程序 - 只是一種形式來處理登記爲員工想一個娛樂中心通行證。我試圖分開一些東西,使它們更清潔。笨定製庫不加載

下面是代碼:

應用/控制器/ reccenter.php

class RecCenter extends CI_Controller { 

    public function register() { 
     $data['title'] = "Utah Valley Rec Center Registration"; 

     $this->output->enable_profiler(TRUE); // Put all the debug statements at the bottom of the page 
     $this->load->helper('html'); 

     $this->load->library('form_validation'); 
     $this->form_validation->set_rules('userRecCenter', 'recreation center selection', 'required'); 

     $userRecCenter = $this->input->post('userRecCenter'); 
     $registerSelf = $this->input->post('registerSelf'); 
     $dependents = $this->input->post('dependents'); 

     if($this->input->post('registerSelf') && !$registerSelf) { // Employee not registering themselves 
      $this->form_validation->set_rules('dependents', 'self selection or dependents', 'required'); 
     } else if($this->input->post('dependents') && count($dependents) == 1 && $dependents[0] == "") { // Employee only registering themselves 
      $this->form_validation->set_rules('registerSelf', 'self selection or dependents', 'required'); 
     } 

     if ($this->form_validation->run() == FALSE) { 
      $this->load->view('templates/header', $data); 
      $this->load->view('pages/reccenter/register', $data); 
      $this->load->view('templates/footer', $data); 
     } else { 
      $this->load->library('Reccenterdao'); 
      $numRows = $this->reccenterdao->getRecCenterInfo(); 
      var_dump($numRows); 

      // Check if already registered 
       // Redirect to different view to change their registration 

      // Do the registration 
       //redirect('reccenter/confirm'); 
     } 
    } 

    public function confirm() { 
     $data['title'] = "Utah Valley Rec Center Registration Confirmation"; 

     $this->load->view('templates/header', $data); 
     $this->load->view('pages/reccenter/confirm', $data); 
     $this->load->view('templates/footer', $data); 
    } 
} 

應用/庫/ Reccenterdao.php

if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Reccenterdao { 

    private $CI; 

    public function __construct() { 
     $this->CI =& get_instance(); 
    } 

    private function connect() { 
     $this->CI->load->database(); 
    } 

    private function disconnect() { 
     $this->CI->db->close(); 
    } 

    public function getRecCenterInfo() { 
     $this->connect(); 

     var_dump("Made it here"); 
     $result = $CI->db->query("SELECT * FROM ucfitness"); 
     var_dump($result->result()); 

     $this->disconnect(); 
     return $result->num_rows(); 
    } 


} 

這裏有幾個額外的配置項,所以你知道我做了什麼來定製它。

的application/config/autoload.php

$autoload['helper'] = array('form', 'url'); 

的webroot/index.php文件(對於環境的定義)

if(isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == "my prod server") { 
    define('ENVIRONMENT', 'prod'); 
} else if(isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == "my stage server") { 
    define('ENVIRONMENT', 'stage'); 
} else { 
    define('ENVIRONMENT', 'test'); 
} 

if (defined('ENVIRONMENT')) 
{ 
    switch (ENVIRONMENT) 
    { 
     case 'test': 
      error_reporting(E_ALL); 
     break; 

     case 'stage': 
     case 'prod': 
      error_reporting(0); 
     break; 

     default: 
      exit('The application environment is not set correctly.'); 
    } 
} 

我也複製到application/config/database.php三個文件夾,application/config/test/database.phpapplication/config/stage/database.php,並application/config/prod/database.php,配置它們各自的連接參數。每個環境都有不同的數據庫服務器和證書。

問題是,當我提交表單並且在表單驗證後使它進入到其他語句$this->form_validation->run()我只能看到空白屏幕。沒有我的絕望var_dump s,沒有。所以,經過我所有的努力和不同的方法來實現這一點,有幾個問題出現了。

  1. 什麼沒有在圖書館工作的?在經歷了所有我能找到的教程/問題之後,看起來我做得很對。
  2. 我如何讓這個其實我可以調試代碼?瀏覽器中沒有錯誤,Apache錯誤日誌中沒有錯誤。我還可以做些什麼?
  3. 將最好的做法是從控制器分離DAO是什麼?從我所知道的情況來看,把這個圖書館變成我想要的圖書館將是最好的主意。它可能只會被這個控制器使用,它將處理註冊和修改註冊。我將編寫幾個其他應用程序,這些應用程序都將位於此CI實例中,但它們將相當分離,並且不需要使用相同的DAO。
  4. 我可以使用首字母大寫的庫文件名,只要它以大寫字母開頭? Reccenterdao.phpRecCenterDAO.php友善得多。

============================================ ================

編輯:我改變了整個事情的模型,但我得到了相同的功能 - 空白屏幕沒有任何錯誤。它似乎加載模型,因爲它在我嘗試實際使用它時與線條一起死亡$numRows = $this->dao->getRecCenterInfo();

我知道現在整件事毫無意義,因爲我沒有將功能用於數據庫或解析用戶輸入。這就是整個問題。在嘗試投入所有業務邏輯之前,我試圖讓它發揮作用。

應用/模型/ rec_center_dao.php

class Rec_center_dao extends CI_Model { 

    public function __construct() { 
     parent::__construct(); 
    } 

    private function connect() { 
     $this->load->database(); 
    } 

    private function disconnect() { 
     $this->db->close(); 
    } 

    public function getRecCenterInfo() { 
     $this->connect(); 

     var_dump("Made it here"); 
     $result = $this->db->query("SELECT * FROM ucfitness"); 
     var_dump($result->result()); 

     $this->disconnect(); 
     return $result->num_rows(); 
    } 
} 

應用/控制器/ reccenter.php有關章節

if ($this->form_validation->run() == FALSE) { 
     $this->load->view('templates/header', $data); 
     $this->load->view('pages/reccenter/register', $data); 
     $this->load->view('templates/footer', $data); 
    } else { 
     $this->load->model('Rec_center_dao', 'dao'); 
     $numRows = $this->dao->getRecCenterInfo(); // This is where it dies 
     var_dump($numRows); 

     // Check if already registered 
      // Redirect to different view to change their registration 

     // Do the registration 
      //redirect('reccenter/confirm'); 
    } 

=============== =============================================

EDIT2 :感謝評論,我發現這是連接到數據庫的問題。當我撥打$this->load->database();時,它會出錯。我已經證實,憑藉我擁有的證書,我可以使用phpMyAdmin管理數據庫(我沒有shell訪問權限來測試)。

的application/config /測試/ database.php中

$active_group = 'default'; 
$active_record = TRUE; 

$db['default']['hostname'] = 'my.hostname.com'; 
$db['default']['username'] = 'db_username'; 
$db['default']['password'] = 'db_password'; 
$db['default']['database'] = 'db' 
$db['default']['dbdriver'] = 'mysql'; 
$db['default']['dbprefix'] = ''; 
$db['default']['pconnect'] = TRUE; 
$db['default']['db_debug'] = TRUE; 
$db['default']['cache_on'] = FALSE; 
$db['default']['cachedir'] = ''; 
$db['default']['char_set'] = 'utf8'; 
$db['default']['dbcollat'] = 'utf8_general_ci'; 
$db['default']['swap_pre'] = ''; 
$db['default']['autoinit'] = TRUE; 
$db['default']['stricton'] = TRUE; 

隨着webroot/index.php上述配置和application/config/test/database.php應該拿起設置爲不同的環境,並相應地照顧它,對嗎?但是,如果我把var_dump放在application/config/database.phpapplication/config/test/database.php中,它根本不會顯示出來。我確認環境設置正確(至少在我正在進行測試時)。

+0

你需要啓用PHP錯誤報告,否則你會得到一個空白的屏幕,CI默認爲'開發'環境,檢查'webroot/index.php'中的「錯誤報告」部分,並適應你的自定義環境。另外爲什麼不使用模型而不是圖書館?最後,你可以讓CI自動管理你的數據庫連接,當然這是你的選擇,如果你想手動完成 –

+0

我以爲我已經做到了。我在上面添加了代碼,以便您可以看到。看起來像我的環境定義邏輯無法正常工作。我認爲讓CI管理連接,所以也許我會再看一遍......現在你說了,模型似乎更有意義,特別是因爲我沒有重用庫。也許我會看看 –

+0

我把它全部改爲模型,但我仍然得到相同的錯誤。我也嘗試改變錯誤日誌記錄以在所有環境中執行相同的'E_ALL',但是我仍然沒有發現任何可以找到的錯誤。 –

回答

0

原來的問題是我的PHP安裝。我正在運行Windows 7並使用MSI安裝程序來安裝PHP 5.2.14。我用php-5.2.14-Win32-VC6-x86.zip來重做安裝。將它解壓縮,放到位,重新配置php.ini,然後運行。

請勿使用MSI安裝程序!

1

當然,你的類有這些錯誤(該代碼的其餘部分看起來很好):

private function connect() { 
    $CI->load->database(); 
} 

應該是:

private function connect() { 
    $this->CI->load->database(); 
} 

既然你已經在構造函數中分配$this->CI,並且您想要使用該類屬性,而不僅僅是新創建的$ CI變量。這同樣適用於斷開()方法,該方法應該是:

private function disconnect() { 
    $this->CI->db->close(); 
} 

然後,在這裏:

public function getRecCenterInfo() { 
    connect(); 

    var_dump("Made it here"); 
    $result = $CI->db->query("SELECT * FROM ucfitness"); 
    var_dump($result->result()); 

    disconnect(); 
    return $result->num_rows(); 
} 

你應該不是(在評論中突出顯示的錯誤):

public function getRecCenterInfo() { 
    $this->connect(); // it's a method in the class, not a regular function 
    $result = $this->CI->db->query("SELECT * FROM ucfitness"); // The property is $this->CI 
    $this->disconnect(); // same as $this->connect(); 
    return $result->num_rows(); 
} 

除了這些錯誤之外,到目前爲止您的類還是沒用的,您可以在常規模型中使用DB類(也許是自動加載)實現相同的功能。

+0

我將我的代碼更改爲您的建議,但仍未解決問題。作爲一個方面說明,我認爲我不能在庫中使用$ this,並且必須使用$ CI,因爲CI處理庫的方式。但是,這對於CI來說是有道理的,然後我仍然可以使用$ this作爲課堂上的東西。 –

+1

您不能使用'$ this'作爲您在模型/控制器和視圖中看到的主要CI對象的引用,但需要使用'$ this',因爲您要引用該類的屬性/方法。 –

+0

明白了。我把所有東西都改成了模型而不是圖書館。請看一下。 –