how to enforce unique entries

4 views
Skip to first unread message

Nicolaas Thiemen Francken - Sunny Side Up

unread,
Oct 21, 2009, 11:47:26 PM10/21/09
to silverst...@googlegroups.com
Hi Everyone

I am curious about unique entries and orphaned records.  A couple of examples:

a. a DataObject based list of towns (how do we prevent the same town from accidentally being added twice).

b. a Many-to-Many relationship between users and ice-cream flavours. How do we make sure the same flavour is not entered twice AND / OR the same combination does not occur twice (or is that automatically enforced).

We could probably write a few lines in onBeforeWrite that checks if the entry exists and stops the writing from going ahead IF the record already exists, but I am wondering if 

a. this is the right way to go about it or should we use Mysql Unique Index?

b. we can create a more universal and faster system 

What about orphaning records (e.g. if you have types of icecream (dataobjects) each of which has many flavours (dataobjects) then what happens if you delete a type of icecream, what will happen to those flavours associated with it, will the user get a warning message, etc...)? Is there / should there be a difference in one-to-many and Many-to-Many?

I am working on ecommerce product variations and for both "unique entries" and "orphan records"  I wonder  "how to do this properly". 

Any thoughts are much appreciated.

The way I see it is that, if you try to enter an already existing record then a friendly message comes up "sorry but a record with ... already exists" and the action is cancelled. If you try to delete a record with children then you get a message stating just that and again the action is cancelled. It would be good however, to enforce these cancellations even  if the action is not coming from the CMS (e.g. from a $obj->delete()).

--
Nicolaas 

Nicolaas Thiemen Francken - Sunny Side Up

unread,
Oct 24, 2009, 2:39:44 AM10/24/09
to silverst...@googlegroups.com
One of the things we could look at is adding referential integrity checks to Hierarchy.php.  Right now, it appears that the ParentID field is hard-coded (assumed it exists) in Hierarchy.php. We could add something like

function onBeforeDelete() {
  if($this->HasChildren()) {
    die("no way san jose");
    //or move children to Hierarchical Parent
    //or move children to Top Level
  }
}

Let me know what you think.

Cheers

Nicolaas
Reply all
Reply to author
Forward
0 new messages