에이바바
에이바바

Reputation: 1031

Table join in CodeIgniter with Active Record and GroceryCrud

Reference: http://www.grocerycrud.com/documentation/options_functions/set_model

I have 2 tables like this:

[tblFitnessClasses] id uid title description location

[tblFitnessClassDateTimes] owner_uid startDate endDate startTime endTime days recurrence

Essentially I want the table to end up like this:

(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  

In part of my main controller I have this:

     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);     
}

In my model I have this:

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

When I run this code I get a table with all of the required fields BUT the fields from table2 (tblFitnessClassDateTimes) are missing all information. The fields do not populate with its data. In addtion to this, if I chose to edit the table it only goes to edit able1(tblFitnessClassses)

enter image description here enter image description here

Upvotes: 1

Views: 2907

Answers (2)

John Green
John Green

Reputation: 58

it is always suggested to use:

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

therefore the code should be:

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

$crud = new grocery_CRUD();

Upvotes: 0

Fad
Fad

Reputation: 9858

You're loading a model the wrong way. In fact, you're not even loading one! You're just creating a new instance of grocery_CRUD and not the model you wrote.

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

Upvotes: 2

Related Questions