Set a relation

In most databases we use database relations - 1:1 1:n n:1 n:m .Below I will explain with an example how we can use the simple 1-1 relation in our CRUD.

We will use two tables . The table employees (the basic table) and the offices table (the relational table). You can see an image of how this two tables have a relation



The primary key of the basic table (employees) and the primary key of the relational table (offices) is recognized automatically . So you need to add only three strings.

  1. The name of the field that we have the relation in the basic table (officeCode )
  2. The relation table (offices)
  3. The 'title' field that we want to use to recognize the relation ( in this example the city)

function employees_management()
{
$crud = new grocery_CRUD();
 
$crud->set_table('employees');
$crud->display_as('officeCode','Office City');
$crud->set_subject('Employee');
 
$crud->set_relation('officeCode','offices','city');
 
$output = $crud->render();
 
$this->_example_output($output);
}
Note: The below example is an iframe so it might appeared with a scroll bar. If you like you can view the example on a new tab

You can add as many relations you need in your crud . You can have for example three relations in one table ( for example project_id, office_id, plan_id ).