⚠ 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

CRUD for tables without primary keys



mephi
  • profile picture
  • Member

Posted 29 May 2012 - 18:44 PM

hi guys,

I have a table MyISAM, without PK column.

Is it possible to use the CRUD on this type of table?

I'm getting this error when it checks for PK column on edit/delete url's from showList function
 PHP Fatal error:  Cannot access empty property in .../application/libraries/grocery_crud.php on line 1368


Looking forward for your reply.

Thanks!

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 29 May 2012 - 20:06 PM

Yes I know. It doesn't support it yet but I want to include it at a future release. Probably it will be included at version 1.2.4 (not at 1.2.3 as I already planned my tasks) . For now a quick solution to solve this kind of issue is to use the set_model method. You can see an example at: http://www.grocerycr...tions/set_model and /topic/90-how-to-create-an-extension-for-grocery-crud-real-example-included/. So in your case your model will look similar to this:


<?php
class MY_grocery_Model extends grocery_CRUD_Model {

function get_primary_key($table_name = null)
{
if($table_name == null)
{
return 'your_field_name'; // <-------- change this line here
}
else
{
$fields = $this->get_field_types($table_name);

foreach($fields as $field)
{
if($field->primary_key == 1)
{
return $field->name;
}
}

return false;
}

}

}


Just change the 'your_field_name' with the field name that you want to have as a key (even if it isn't). I think that you will not have any problem with this.

mephi
  • profile picture
  • Member

Posted 30 May 2012 - 15:24 PM

Hey John, it worked like a charm.

Thanks for the quick reply!