Can someone explain what set_relation() and set_relation_n_n in grocery crud for codeigniter does?

551 views Asked by At

I'm trying to integrate the grocery crud a system but I don't understand what set_relation my database design already has relations so I want to know how this will help, or rather what it does. I can't understand the docs' examples.

DOCS EXAMPLES

void set_relation( string $field_name , string $related_table, string $related_title_field [, mixed $where [, string $order_by ] ] ) Quick Description: Set a relation 1-n database relation. 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. An example of this:

$crud->set_relation('user_id','users','username');

You can have as many fields you like to call from the other table and the syntax is really simple. Just at the 3rd field you will have the symbol { and } . So it will be for example:

$crud->set_relation('user_id','users','{username} - {last_name} {first_name}');

And you can have whatever syntax or symbols you like. So for example you can have:

$crud->set_relation('user_id','users','{username} ( {last_name} {first_name} )');

The parenthesis is just to show you that you can insert whatever symbol you like.

1

There are 1 answers

0
abdullah yahya On

the $crud->set_relation function used to represent [one to many relation]

and the $crud->set_relation_n_n function used to represent the [many to many relation]

for example the gender may be male or female exclusively It cannot be both So we can represent it using set_relation; but the student courses for example may be more than one and need new table to connect studentID with many coursesID's like this example :

***tblStudents***
id      name
1       Jack
2       John
-------------------------------
***tblCourses***

id   courseName
1     Math
2     Science
3     Statistics
4     Geographic
-------------------------------
***tbl_stu_course_m_m***
 
studentID  courceID
1            1
1            2
1            4

in this case Jack with id 1 has relation with three courses from total (four courses) if we represent them in malty select options in insert or edit mode, will be like this: Courses:

  • Math
  • Science
  • statistics[not selected]
  • Geographic