Search code examples
phpcodeignitergrocery-crud

Set relations on GroceryCRUD with Codeigniter


I am working on a projecj with codeigniter, and it's my first time using GroceryCRUD, and I need to make that a patient can have any number of reports, I have this on my database.

enter image description here

Where idpatients is the foreign key.

I am using this on my controller

public function reports() 
{
    $crud=$this->grocery_crud;
    $crud->set_table('reports');
    $crud->set_subject('Reports'); 
    $crud->set_language('english');
    $crud->set_relation('idpatients','patients','Patient');
    $output=$crud->render();
    $this->load->view('admin_reports', $output);
}

And I get this error

enter image description here

Any help would be appreciated to fix it, How can I declare the relationship so when I add a new report I can choose the patient from a dropdownbox or something?

UPDATE

When I change db_debug for false I get this error

enter image description here


Solution

  • Based on documentation:

    void set_relation( string $field_name , string $related_table, string $related_title_field [, mixed $where [, string $order_by ] ] )

    Set a relation 1-n database relation. This will automatically create a dropdown list to the fields and show the actual name of the field and not just a primary key to the list.

    Which means, it will display the field from the table other then primary key (and you are trying to display field Patient which is not present in patients).

    Solution for your issue - replace Patient with Name (or some other field which is present in patinets table):

    $crud->set_relation('idpatients','patients','Name');