Plugin::SimpleCRUD: primary key missing from edit form
Terr! I played a bit with SimpleCRUD plugin and noticed on debug output, that every time i submit edit form, there is made an INSERT. AFAIU, insert is choosed if primary key is missing. Here is output of a simple edit form, i don't see a primary key here: <!-- Generated by CGI::FormBuilder v3.0501 available from www.formbuilder.org --> <form method="post" onsubmit="return validate(this);"> <div><input id="_submitted" name="_submitted" type="hidden" value="1" /></div> <table> <tr valign="top"> <td><b>Keel</b></td> <td><input id="keel" name="keel" type="text" value="udmurdi" /></td> </tr> <tr valign="top"> <td align="center" colspan="2"><input id="_submit" name="_submit" type="submit" value="Submit" /></td> </tr> </table> </form> Primary key for this simple table is 'ke_nr' (and other columns in 'keel', thats it) and it is set for simple_crud-method as that: simple_crud( record_title => "keel", prefix => "/keel", db_table => "keel", sortable => 1, paginate => 30, key_column => 'ke_nr', downloadable => 1, editable => 1, deletable => 1, ); Why PK is missing from edit form? -- Wbr, Kõike hääd, G
2011/10/20 WK <wanradt@gmail.com>:
Why PK is missing from edit form?
I misunderstood a bit, i see now the id is get from posting URL, so this is not the problem. Problem is: i have primary key not 'id' but 'ke_nr', if i change my table having pk as 'id' (and according option for simple_crud), editing works fine. I think the problem is here: Line 462: if (exists params->{$key_column}) { # We're editing an existing record $success = database->quick_update($table_name, { $key_column => params->{$key_column} }, \%params); $verb = 'update'; } else { $success = database->quick_insert($table_name, \%params); $verb = 'create new'; } It should begin: if (exists params->{id}) { And then something like: $params{$key_column} = $params{id}; delete $params{id}; Maybe it is cleaner to make attribution to right hash key somewhere earlier, then if-block may remain same. -- Wbr, Kõike hääd, Gunnar
2011/10/20 WK <wanradt@gmail.com>:
It should begin: if (exists params->{id}) {
I changed it to: Line 462: if (exists params->{id}) { # We're editing an existing record $success = database->quick_update($table_name, { $key_column => params->{id} }, \%params); $verb = 'update'; } else { Works for me. Sorry for messing here solo! -- Wbr, Kõike hääd, G
On Thursday 20 October 2011 14:49:19 WK wrote:
2011/10/20 WK <wanradt@gmail.com>:
It should begin: if (exists params->{id}) {
I changed it to: Line 462: if (exists params->{id}) {
# We're editing an existing record $success = database->quick_update($table_name, { $key_column => params->{id} }, \%params); $verb = 'update'; } else {
Works for me. Sorry for messing here solo!
That looks to be a sensible and sane fix - thanks for catching, reporting and fixing this issue! I'll get a new version out soon with that change. I'll also see if I can handle Issue 11 at the same time: https://github.com/bigpresh/Dancer-Plugin-SimpleCRUD/issues/11 To support multiple keys, I think I might need to add an "editing" flag rather than just test for the presence of the 'id' field; then, I can add each of the key columns to the URL (or perhaps as hidden form fields), use them all in the WHERE clause of the update - for e.g., if simple_crud() was called with key_column => [qw/key_1 key_2 key_3/], then that section of editing code would become something like: if (params->{simplecrud_mode} eq 'editing') { my %where; my @key_cols = ref $key_column ? @$key_column : ($key_column); for my $key_col (@key_cols) { $where{$key_col} = params->{$key_col}; } $success = database->quick_update($table_name, \%where, \%params); $verb = 'update'; } At a quick glance, a change similar to above should take care of both the problem you were seeing, and saberworks' need for composite keys. -- David Precious <davidp@preshweb.co.uk> (bigpresh) http://www.preshweb.co.uk/ "Programming is like sex. One mistake and you have to support it for the rest of your life". (Michael Sinz)
participants (2)
-
David Precious -
WK