使用Active Record和GroceryCrud在CodeIgniter中连接表

时间:2012-05-04 14:18:55

标签: php mysql codeigniter activerecord

参考:http://www.grocerycrud.com/documentation/options_functions/set_model

我有2张这样的表:

  

[tblFitnessClasses] id uid标题描述位置

     

[tblFitnessClassDateTimes] owner_uid startDate endDate startTime   endTime天重复

基本上我希望桌子最终像这样:

(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;
        }
}
?>

当我运行此代码时,我得到一个包含所有必填字段的表但是table2(tblFitnessClassDateTimes)中的字段缺少所有信息。这些字段不会填充其数据。除此之外,如果我选择编辑表格,它只会编辑able1(tblFitnessClassses)

enter image description here enter image description here

2 个答案:

答案 0 :(得分:2)

你正在以错误的方式加载模型。事实上,你甚至没有加载一个!您只是创建grocery_CRUD的新实例,而不是您编写的模型。

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

答案 1 :(得分:0)

始终建议使用:

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

因此代码应为:

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

$crud = new grocery_CRUD();