In case you've missed it, you are looking at an older version of the website. Checkout our latest version, we promise you will love it 😍

set_primary_key

void set_primary_key( string $primary_key_field [ , string $table_name ] )
Quick Description: Handles the default primary key for a specific table.
Available for version >= 1.2.3

Handles the default primary key for a specific table. If the $table_name is NULL then the primary key is for the default table name that we added at the set_table method. Really useful for the mysql VIEW tables.

At the below example we have the table countries that has the below fields:

  • country_id (primary key)
  • country_code
  • country_title

Let's say that we don't want to relate our table with the country_id but with the country_code as it also unique. At this situation we can do something similar to this:

 
function employees_management()
{
    $crud = new grocery_CRUD();
 
    $crud->set_table('employees');
    $crud->set_subject('Employee');
 
    $crud->set_primary_key('country_code','countries');
    $crud->set_relation('country','countries','country_title');
 
    $output = $crud->render();
 
    $this->_example_output($output);
}
 

With the above example at the country field will be stored the country_code and not the country_id

At this example the:

$crud->set_primary_key('country_code','countries');

simply means that when you request the primary key from the table countries it will give you the country_code instead.