[solved] Auto increment ID field
Hello to everybody,
I was wondering if it's possible to create an auto-incrementing ID editable field in Catalog. I see that Catalog automatically created an ID field, which is auto-incremental but not editable. I managed to add another ID field, which is editable but not auto-incremental. Is it possible to have both features?
Thank you in advance
Re: Auto increment ID field
I don't see a possible configuration for you.. the ID from the catalog Items is set in the database.. its not usefull to change it. I think you must write your own extension for that.
regards
Re: Auto increment ID field
I think the best thing would be to be able to create a new text field giving as default value the ID field of the record itself.
For example, if we already have 3 records, the fourth would be
ID: 4
copied-ID: 4
Then the user can edit the copied-ID as it suits him. I don't need specific integrity checks on this field, it's up to the user to insert the right values. This is because the copied-ID is used for different editions of the same product, therefore it must be absolutely free.
Do you think this would be easier to achieve? Do I still need a new extension for that?
Thank you
Re: Auto increment ID field
I think you will need an extension for the copy job..
regards
Re: Auto increment ID field
You're right, that's what I did.
I created an extension called catalog_auto_insert_id and hooked it to catalogFrontendInsert using
Code:
$GLOBALS['TL_HOOKS']['catalogFrontendInsert'][] = array('catalog_auto_insert_id', 'insert_id');
in config.php
This is the code I wrote for the extension in catalog_auto_insert_id.php
Code:
<?php
class catalog_auto_insert_id extends Frontend {
/*
* Marco Zanetti - 2013-03-12
* Questa funzione viene chiamata ogni volta
* che un record viene inserito. Nel caso in cui
* il record venga inserito nella tabella
* va a salvare l'ID massimo precedentemente inserito
* per tale gruppo nel nuovo record
* L'ID viene salvato nel campo id_edizione che non ha
* vincoli di integrità . In questo modo l'utente può editarlo
* come preferisce senza andare ad intaccare l'ID vero e proprio del record
*
* This function is called each time
* a record is inserted. It takes the max ID previously
* inserted for the same tipology
* and uses that ID + 1
* The new ID is saved in the id_edizione field so it can be
* edited by the user while not changing the real record ID
*/
public function insert_id(&$strArticle, $objModule, $strTable)
{
if($strTable=='cat_edizioni' && is_null($strArticle['id_edition']))
{
//Extracts max numeric ID inserted for selected tipology
//Estrae il massimo ID numerico inserito finora per quella tipologia di record
$idToInsert = $this->Database->prepare("SELECT MAX(CAST(id_edition AS SIGNED INTEGER)) as id_to_use from ".$strTable." WHERE tipologia=?")->execute(intval($strArticle['
//We pick next counter
//Aumenta di uno il contatore
$idToInsert->id_to_use = ($idToInsert->id_to_use) + 1;
//Updates table edition ID with the new one
//Update dei dati in tabella con il nuovo ID
$objUpdatedItem = $this->Database->prepare('UPDATE '.$strTable.' set id_edition='.$idToInsert->id_to_use.' WHERE id=?')->execute($strArticle['id']);
}
}
}
?>
and it works! :)
Re: Auto increment ID field
Nice!
Thx for sharing this ;-)
regards