jo-ka
Member
Hello.
I'm trying to find the best way to achieve this:
I have a table with players, where I have a field with license number. I need to automatically give the player a number, when the record is saved, avoiding this way that if more than one user is adding players, the number never get repeated.
I'll use later some list plugins like is unique or so, but now I'm just trying to find how to do this.
My approach is to have a calc element (only calc on save), selecting the latest record on the table and increment the value by one, then use upsert to update the licence field.
I have this calc:
$db = JFactory::getDBO();
$sql="SELECT MAX(license)+1 AS next_number FROM tests";
$db->setQuery($sql);
return $db->loadResult();
With this I can get the last number and generate a new one, which I think it's ok, but then I'm missing something on upsert.
The question here is that the license number must be only inserted on new records, and not on updated ones.
Am I thinking correctly with this approach? Is there a better way? What can probably be wrong on my upsert?
BR
I'm trying to find the best way to achieve this:
I have a table with players, where I have a field with license number. I need to automatically give the player a number, when the record is saved, avoiding this way that if more than one user is adding players, the number never get repeated.
I'll use later some list plugins like is unique or so, but now I'm just trying to find how to do this.
My approach is to have a calc element (only calc on save), selecting the latest record on the table and increment the value by one, then use upsert to update the licence field.
I have this calc:
$db = JFactory::getDBO();
$sql="SELECT MAX(license)+1 AS next_number FROM tests";
$db->setQuery($sql);
return $db->loadResult();
With this I can get the last number and generate a new one, which I think it's ok, but then I'm missing something on upsert.
The question here is that the license number must be only inserted on new records, and not on updated ones.
Am I thinking correctly with this approach? Is there a better way? What can probably be wrong on my upsert?
BR