Primary Key required to Auto Increment?

stephenfsnow 29 Mar, 2013
I've created a form that creates a unique 'id' number based on the month & year entered by the user as well as the user id number...
<?php
$form->data['id'] = "{$form->data['cfu_id']}{$form->data['year']}{$form->data['month']}";
?>

Anyway, in the database table I've set the column 'id' as primary and as an INT. However, if I don't set it to auto increment, no records are saved. Curiously, when I set 'id' to auto_increment, the form will save to the database but only once, it won't seem to create any more records even when a different month & year are used. In the initial record, 'id' is in the correct format reflecting values for 'cfu_id''year''month'.

The reason I want to use this custom 'id' as the primary key is because I want every registered user to be able to create only one unique record for each month/year.

P.S. ChronoForms is awesome, love your work!
GreyHead 29 Mar, 2013
Hi stephenfsnow,

I'm not sure of the answer to this, I generally keep to the auto-increment as the Primary Key and put anything else in another table. I don know that the Joomla! code that ChronoForms uses to save only works with a numeric Primary Key but you have that. Have you tried setting the column to 'Unique' as well?

Otherwise I'd add the data in a separate column (or columns) and use a MySQL query in a Custom ServerSide validation action to check if a record already exists.

Bob
stephenfsnow 29 Mar, 2013
Thanks for the information.

I used the route you suggested and that works (after grabbing some code in another thread). Now I'm just trying to figure out how to alter it so that record is updated if there is a record where ts_id already exists...

<?php
$cektsid = '';
if ( isset($form->data['ts_id']) && $form->data['ts_id'] ) {
   $cektsid = trim(strip_tags($form->data['ts_id']));
}
$db =& JFactory::getDBO();
$query = "
    SELECT COUNT(*)
        FROM `#__chronoforms_data_employee_timesheet`
        WHERE `ts_id` = '{$cektsid}' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count > 0 ) {
   UPDATE RECORD
}
?>


I know I need something in the "UPDATE RECORD" area, just not sure what yet.
GreyHead 31 Mar, 2013
Hi stephenfsnow,

I think I would get the record ID instead of the count, then you can use that plus a DB save action to update the record.
<?php
$cektsid = '';
if ( isset($form->data['ts_id']) && $form->data['ts_id'] ) {
   $cektsid = trim(strip_tags($form->data['ts_id']));
} else {
  // quit if there is no value;
  return false;
}
$db =& JFactory::getDBO();
$query = "
  SELECT `cf_id`
    FROM `#__chronoforms_data_employee_timesheet`
    WHERE `ts_id` = '{$cektsid}' ;
";
$db->setQuery($query);
$form->data['cf_id'] = $db->loadResult();
?>

Bob
stephenfsnow 01 Apr, 2013
Exactly what I was needing, works perfectly. Thank you for your help.

P.S. Subscription purchased (and is a bargain IMHO)
This topic is locked and no more replies can be posted.