Problems, need help? Have a tip or advice? Post it here.
9 posts Page 1 of 1
Is there a way to change how the id numbers for pages are created?

I have a database of bottles for a bottle collection I have, and when adding an item right now the ID numbers would go like this: "1, 2, 3, 4...". But I want the IDs to have a specific format like this: "001, 002, 003, 004..." instead (leading zeros basically). Is there a way to do this? I'd be fine if it requires a new field too. I just want it to automatically be numbered when I add a new item so i dont need to remember where I left off.

Thanks!
Hi,

The ID that Couch generates is actually provided by the database (i.e. MySQL) and is used internally to uniquely identify a page across templates - which means that 1, 2, 3 could be given to pages of blog.php and then 4, 5, could be allotted to news.php with 6 going back to blog.

So the IDs are not guaranteed to be sequential. Also the IDs are not reusable i.e. if you delete a page with ID 6 and recreate a new page with the same name it will have a new numeric ID.

Point of the explanation was that, I think, the Couch ID you are targeting is not suitable for your purpose.

Speaking of which, could you please let us know how you propose to use the automatic generated sequential ID you mentioned? I.e. do you plan to use it in the URL of the products? or just as a SKU?

Thanks.
I'm going to be using it just as a Sku number. I have a bottle collection and I just want to take the id that couch provides and attach it physically with a label to the bottle it belongs to.

If I can't do leading zeros thats fine, its more of just a preference for me, and for future knowledge if i ever need it for sure.

For this case I dont really need to reuse the numbers, but I would prefer it not skip numbers simply because a page in a different area of the site was added.
OK, got it.
Can be done with a bit of programming - we can keep the last assigned ID saved and them allot the next ID by adding 1 to it and saving it as the last assigned.

Just one thing to note - the IDs will not be reusable i.e. if a bottle with ID 036 happens to be deleted and if the last ID at that time is 057, the next bottle will be 058 and the deleted 036 will remain a hole in the sequence. Would that be fine?

Please let me know. It is weekend but I'll try to cook something up as soon as I get back to work.
That would work perfectly :)
Hi,

Here is the promised solution -

N.B: You'll have to upgrade to the 1.4.5x version of Couch to implement this solution. You can download those versions from viewtopic.php?f=5&t=8581 or viewtopic.php?f=5&t=8981


The solution involves defining a simple text field to hold the SKU and then attaching a custom 'validator' to it that would fill the field with the calculated sku.

Define a region in your template like this (please copy/paste and use this exact definition) -
Code: Select all
<cms:editable name='sku' label='SKU (will be generated automatically)' type='text' validator='set_sku' >0</cms:editable>  

The field above has a validator named 'set_sku'. Validators are simple PHP functions. Let us now create that.

Edit your 'couch/addons/kfunctions.php' file (you might have to rename the 'kfunctions.example.php' to 'kfunctions.php') and add the following to it -
Code: Select all
// Custom validator for field
function set_sku( $field, $args ){
    global $FUNCS;
   
    if( $field->modified ){ // not allowed to edit this field
        $field->data = $field->orig_data;
        $field->modified = 0;
    }
   
    // set a SKU if 0 (can happen only when new page gets created)
    $sku = trim( $field->get_data() );
    if( !$sku ){
        $field->page->_lock_template();
       
        $sku = intval( $FUNCS->get_setting('my_sku') );
       
        $sku++;
       
        $field->store_posted_changes( sprintf('%03d', $sku) ); // format with leading zeros
        $FUNCS->set_setting('my_sku', $sku );
    }
   
    return true;
}

Now everytime you create a new page, it should get a sequentially increasing sku e.g. 001, 002 etc.

Hope it helps. Do let me know.

Thanks.
Thanks for the help, it looks good! :)
Hey,

One question.... I use this solution in one of my projects. At the beginning I have set the sku to 3 digits, after a period of time I have realised that those 3 digits are not enough for the sku, so I opted for 7 digits. In some cases I order the pages by the sku and because there are 3 digit and 7 digit skus the ordering is not working as it should. I tried to manually update the old, 3 digit skus but the fields won't update with the newly (manualy) entered values.
Is there a way to change the values contained by the sku field?

Thanks.
The sampled field doesn't have the search_type defined as 'integer' or 'decimal'. Very probably, sorting is messed because of this. A change, however, requires re-saving of every existing page. At this point it is maybe more profitable in the long run to switch to the UID addon, which works really good, auto-expands and can be used for ordering.

atisz wrote: Hey,

One question.... I use this solution in one of my projects. At the beginning I have set the sku to 3 digits, after a period of time I have realised that those 3 digits are not enough for the sku, so I opted for 7 digits. In some cases I order the pages by the sku and because there are 3 digit and 7 digit skus the ordering is not working as it should. I tried to manually update the old, 3 digit skus but the fields won't update with the newly (manualy) entered values.
Is there a way to change the values contained by the sku field?

Thanks.
9 posts Page 1 of 1
cron