⚠ 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

Non auto increment primary key



simonfrank
  • profile picture
  • Member

Posted 01 May 2012 - 09:32 AM

First things first AWESOME product, definately making a donation. Really, really clever stuff, I was building a CRUD and it was messy, so discovering this , I stopped. It was only after I started coding and found the secret weapon of set_relation_n_n which is just superb.

I have to use UUIDs as my primary key on some of my tables because I am using some unions and haven't found a a way to make unions work with auto increment.

I have experimented with different ways ofhaving a UUID as primary key and found that using an add callback is best (I don't whether it is but using the value on the hidden field type overrides it on edit. So I have


$crud->callback_add_field('rule_id',array($this,'rule_uuid_callback'));


and my callback is

function rule_uuid_callback() {
return'<input type="hidden" name="rule_id" value="'.$this->crud_model->uuid().'">';
}

$this->crud_model->uuid() selects a UUID from MySQL.

This might be a silly way of doing it but I have not found a way to do an auto UUID in MySQL.

When you save the record the prompt appears at the bottom of the screen with the following code

<p>
Your data has been successfully stored into the database.
<a href="http://127.0.0.1/~simonfrank/iresponse/index.php/application/rules/edit/0">Edit rule</a>
or
<a href="http://127.0.0.1/~simonfrank/iresponse/index.php/application/rules">Go back to list</a>
</p>

(notice the 0 after edit)

This on its own is not really a hassle as I can CSS hide the first link. Where it is much more difficult is if I am using set_relation_n_n when the record is saved as it does not know the primary key (even though it is in the the field and in the post array). I therefore end up a 0 key in my lookup table for rule_id.

I have considered using before insert callabcks to set the primary key but don't know how to do this having skimmed through the source code.I also considered having an after insert callback to redirect back to edit i.e. have a smaller add screen with no set_relation_n_n and then when saved redirect to an edit screen with all fields - clunky but it would work (I think).

Any suggestions, work arounds ? (if it is me I apologise in advance, but I have tried searching and varying experiments to try and address it.

carlinchisart
  • profile picture
  • Member

Posted 02 May 2012 - 16:28 PM

hi @simonfrank, i have to do a similar work, in one proyect i have to creade the primary key, so i used the funcition calbalk_before_insert http://www.grocerycrud.com/documentation/options_functions/callback_before_insert

and return the array with the primary key something like:


$post_array['primary_key']=yourPrimaryKey;
return $post_array


so i have problem with the n-n relation, when i add a valua the table with n-n relation no save the data, so i use de funcition calbaclk_after_insert http://www.grocerycrud.com/documentation/options_functions/callback_after_insert
and i insert the values in the tabla n-n manually.

i don't know if this solution work for you.