Unique keys for custom blocks
Permalink
Newbie question. I have successfully built and entered data in a custom block. The block allows a user to enter detail information on a hike. The database is defined with a unique identifier for each hike (hike_id) that will allow me to link the main hiking table (created in this custom block) to other tables for advanced searches (1 to many situations). For example linking the hike to a related table of nearby towns, ratings, etc.
When I go to update the block I get an error indicating there is a duplicate key. After looking around the forums and rereading the documentation I now understand that Concrete5 creates a new data record everytime block cnotent is updated.
The block was built with a unique index in the db.xml
<index name="hike_id">
<col>hike_id</col>
<UNIQUE/>
</index>
This obviously will not work if Concrete5 is creating new records for each update. So my question is how can I enforce a unique key during the data entry process? Is there a way in the auto.js file during an add (as opposed to an edit) to query the block's table and determine that the new key (hike_id) is unique? This query must obviously only search through "approved or current" records.
I have come across an option to set btIncludeAll to 1 and turn off the block updating behavior but that does not seem like a good idea.
Is there a better way to attack this problem? What is the best practice for requiring users to enter a unique identifier when entering new data records? My quess is that this is a problem for ecommerce and other applications that require unique product id's etc.
Thank you.
When I go to update the block I get an error indicating there is a duplicate key. After looking around the forums and rereading the documentation I now understand that Concrete5 creates a new data record everytime block cnotent is updated.
The block was built with a unique index in the db.xml
<index name="hike_id">
<col>hike_id</col>
<UNIQUE/>
</index>
This obviously will not work if Concrete5 is creating new records for each update. So my question is how can I enforce a unique key during the data entry process? Is there a way in the auto.js file during an add (as opposed to an edit) to query the block's table and determine that the new key (hike_id) is unique? This query must obviously only search through "approved or current" records.
I have come across an option to set btIncludeAll to 1 and turn off the block updating behavior but that does not seem like a good idea.
Is there a better way to attack this problem? What is the best practice for requiring users to enter a unique identifier when entering new data records? My quess is that this is a problem for ecommerce and other applications that require unique product id's etc.
Thank you.
Ideally, the block table would hold all the information you want to display. If you need to keep additional information in a separate table, then the block's table should only contain the hike_id. All other information would be pulled from your second table. I would setup the hike_id in this secondary table to auto-increment (see below). Then during the save process, I would check if a hike_id is present. If not, then I would run an insert command. Otherwise I would just run an update command.
<field name="hike_id" type="I">
<key/>
<unsigned/>
<autoincrement/>
</field>
Here is a great reference for creating the db.xml file:http://phplens.com/lens/adodb/docs-datadict.htm...
As a newbie, you may also want to try the Designer Content add-on by jordanlev:http://www.concrete5.org/marketplace/addons/designer-content/...