⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

set_relation_n_n not saving primary key on add record with non auto increment primary key



simonfrank
  • profile picture
  • Member

Posted 14 June 2012 - 13:01 PM

Hi all, I have searched the forum and tried several approaches to fix this but still no joy.

Essentially if using a UUID as the primary key of the main table, if adding a record the relationship is not saved. There are entries in the relationship table but the primary key is 0. If you edit the record and save all is fine. Is there any way to hack this to work? I have tried callback_add field to set the UUID of the primary key and call_back_before_insert.

Here is my sample code fields have been renamed to simplify


$crud = new grocery_CRUD();
$crud->set_table('items');
$crud->set_subject('items');
$crud->columns('name', 'description');
$crud->fields('item_id','name','description','company_id','url','sub_items');
$crud->change_field_type('company_id','hidden');
$crud->change_field_type('item_id','hidden');
$crud->change_field_type('name','string');
$crud->unset_texteditor('description','full_text');
$crud->unset_texteditor('url','full_text');
$crud->callback_add_field('item_id',array($this,'item_id_callback'));
$crud->callback_add_field('company_id',array($this,'company_id_callback'));
$crud->set_relation_n_n( 'sub_items','items_to_sub_items','sub_items','item_id','sub_item_id','sub_item_name','priority');
$crud->set_theme('flexigrid');
$crud->where('form_capture_items.company_id',$this->session->userdata('company_id'));
$crud->callback_before_insert(array($this,'add_uuid_to_item_id'));
$markup = $crud->render();
$markup->main_content='gc_view';
$markup->title='Test';
$this->load->view('includes/main_template',$markup);

//$this->crud_model->uuid() uses MySql to generate a UUID

//This was my first attempt
function item_id_callback() {
return'<input type="hidden" name="item_id" value="'.$this->crud_model->uuid().'">';
}
//This was my second attempt
function add_uuid_to_item_id($post_array) {
$post_array['primary_key']=$this->crud_model->uuid();
return $post_array;
}