Re: [dotcms] Auto incremented structure field

39 views
Skip to first unread message

Mark Pitely

unread,
Aug 21, 2012, 11:42:18 AM8/21/12
to dot...@googlegroups.com
Florian,
Content added to the system is assigned a unique Global Identifier, which is not sequential, and for most purposes is superior to an autoincrement for accessing a specific piece of data.

When you do a dotContent pull, you can order by iDate (which is, as far as I can tell the initial creation date) and then use a counter within the loop to generate fake numbers that would be consistent. modDate would work as well, but that changes when an item is touched, so is less perfect for long term referrals.


Something like this:

#set ($id=$request.getParameter("id"))

#set ($counter=0)
#foreach($con in $dotcontent.pull("+structureName:Viewbook",0,"Viewbook.iDate desc"))
##Put this later in the loop if you want to start with an item 0
#set ($counter=$counter+1)

#if ($UtilMethods.isSet($id))
#if ($counter==$id)
##THIS IS THE SELECTED ITEM
$con.title
#end
#end

##If no id, display a list of all the items with their id.
#if (!UtilMethods.isSet($id))
<a href="?id=$counter">$con.title</a><br>
#end


#end

But what really matters is WHY you need these integer autoincrements? This would give you a code that would be consistent across time unless a piece is unpublished/deleted. You could actually count the unpublished ones as well, as your pull can get the unpublished items and count through them but only show published items. This would also insure no 'gaps' when an item is removed.



Mark Pitely
Marywood University

On Tue, Aug 21, 2012 at 11:15 AM, <florian...@gmail.com> wrote:
Hi,
I'm trying to add a field to a content structure with an auto increment field starting at 0 and then incrementing by 1 each time a new content is saved.
How would you do this as there is no dedicated field type available?

Thanks,
Florian

--
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/dotcms/-/dEE3m07SyfgJ.
To post to this group, send email to dot...@googlegroups.com.
To unsubscribe from this group, send email to dotcms+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/dotcms?hl=en.

Alex

unread,
Aug 21, 2012, 12:30:59 PM8/21/12
to dot...@googlegroups.com
You need to create a custom field.  Here is what I did to generate a unique sequential id for a structure that publishes classifieds.  I set it to start with 100, but you can set it to 0 too.


-----custom field starts here ----
#set ($_dummy = $render.eval("\#set ($CustomFieldValue = $${field.velocityVarName})"))
#if($CustomFieldValue == "")

#set($stObj = $structures.findStructure($field.structureInode))
#set($stName =$stObj.name)

#set($ClassifiedsList = $dotcontent.pull(
"+structureName:$stName +(conhost:HOSTID conhost:SYSTEM_HOST) +live:true +working:true",0,"modDate desc"))

#if($ClassifiedsList.size() == 0 )
#set($display_ID = 100)

#elseif($ClassifiedsList.size() > 0 )
#set($max = 100)

#foreach($con in $ClassifiedsList)
#if($math.toInteger($con.listingId) > $math.toInteger($max))
#set($max = $con.listingId)
#end
#end
#set($display_ID = $math.add($max,1))
#end





  <script type="text/javascript">
    function updateListingId(){
  
  dojo.byId("display_structure_ID").innerHTML $display_ID;
  dojo.byId("listingId").value=$display_ID;
    }
    dojo.addOnLoad(updateListingId);
  </script>



<div id="display_structure_ID"></div>
#else
<div id="display_structure_ID">$CustomFieldValue</div>
#end
-----custom field ends here ----
Reply all
Reply to author
Forward
0 new messages