2012-05-04 42 views
1

參考:http://www.grocerycrud.com/documentation/options_functions/set_model表CI中加入與活動記錄和GroceryCrud

我有2個表所示:

[tblFitnessClasses] id uid title description location

[tblFitnessClassDateTimes] owner_uid startDate endDate startTime endTime days recurrence

基本上我想表落得這樣的:

(uid - hidden) | Title  | Description | Location | Start Date | End Date  | Start Time | End Time | Days  | Reccurence 
       Swim Lesson Level 1   Gym   05/04/2012  NULL   12:30  1:30  Mon,Wed,Fri 2 

在我的主控制器的一部分,我有這個:

 function fitnessSchedule() 
{ 
    $this->config->set_item('url_suffix', ''); 
    $crud = new grocery_CRUD(); 
    $crud->set_table('tblFitnessClasses'); 
    $this->load->model('schedule_model'); 
    $this->schedule_model->join_table('tblFitnessClasses','tblFitnessClassDateTimes'); 
    $crud->columns('title','description','location','startEventDate','endEventDate','startTime', 'endTime', 'days', 'recurrence', 'finalDate); 
    $crud->display_as('title','Event') 
     ->display_as('description','Description') 
     ->display_as('location','Location') 
     ->display_as('startEventDate','Start Date') 
     ->display_as('endEventDate','End Date') 
     ->display_as('startTime','Start Time') 
     ->display_as('endTime','End Time'); 
    $crud->required_fields('title','location'); 
    $crud->set_subject('Event');    

    $output = $crud->render(); 
    $this->_example_output($output);  
} 

在我的模型我有這樣的:

<?php 
class schedule_model extends CI_Model 
{ 
     public function join_table($table1,$table2) 
     { 
      $this->output->enable_profiler(TRUE);//Turns on CI debugging 

      $this->db->select("*"); 
      $this->db->from($table1); 
      $this->db->join($table2, $table1.".uid". "=".$table2.".owner_uid"); // Join classes and class date times by UID 

      $results = $this->db->get()->result(); 
      return $results; 
     } 
} 
?> 

當我運行這段代碼,我得到一個表,所有必填字段,但是從表2中的字段(tblFitnessClassDateTimes)缺失的所有信息。這些字段不會填充其數據。在addtion到這一點,如果我選擇了編輯表那就只編輯able1(tblFitnessClassses)

enter image description here enter image description here

回答

2

你加載模型中的錯誤的方式。事實上,你甚至沒有加載一個!你只是創建了一個新的實例grocery_CRUD而不是你寫的模型。

$this->load->model('schedule_model'); 
$this->schedule_model->join_table('tblFitnessClasses','tblFitnessClassDateTimes'); 
+0

變更後它說:「致命錯誤:類 'grocery_CRUD_Model' 未發現在C :\ xampp \ htdocs \ codeigniter \ application \ models \ schedule_model.php on line 3「(我在模型文件夾中有'grocery_crud_model.php')非常感謝您清除其他錯誤! –

+1

@에이바您需要擴展'CI_Model'而不是'grocery_CRUD_Model'。你不能擴展你寫的另一個模型。我建議你看看Grocery CRUD文檔,特別是這個http://www.grocerycrud.com/documentation/codeigniter_installation和這個http://www.grocerycrud.com/documentation/create-crud-codeigniter-tutorial –

+0

有趣,在它說擴展grocery_CRUD_Model(以及擴展CI_Model的代碼)的例子中。我會看看文檔。再次感謝。 –

0

它總是建議使用:

$this->load->library('grocery_CRUD'); 

因此代碼應該是:

$this->load->library('grocery_CRUD'); 

$crud = new grocery_CRUD();