Jump to:

17452 Posts in 4473 Topics by 1971 members


SilverStripe Forums » Archive » secondary indices in tables

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1276 Views
  • jahbini
    Community Member
    13 Posts

    secondary indices in tables Link to this post

    I need to create a dictionary of phrases. Each phrase must be unique in the dictionary: That is, it looks like an index to the table. The phrase will have many-to-many relations with other classes in my whole site, and may be a real source of inefficiency unless speedy look up to insure uniqueness, etc. (oh, yes, there will be potentially many, many thousands of these phrases)

    At present I see no way to tell sapphire to create a second index, nor do I see an easy way to insert into an existing table and insure uniqueness.

    I fear that I have looked in all the wrong places and am missing something pretty fundamental. Is there an 'industry standard' method of generating secondary keys? And insuring unique entries in a DB?

  • jahbini
    Community Member
    13 Posts

    Re: secondary indices in tables Link to this post

    After drilling down into Sapphire, I found some hints on this subject. Evidently secondary indexes are already in sapphire, bug no examples seem to exist. This snippet will force that one and only one item of 'Tag' exists for each Tagname.

    Also the syntax for secondary keys is, well, not obvious.
    $indexes = array('Bob' => 'unique (Joe)'); -- gets mangled to 'UNIQUE BOB (JOE)'


    class Tag extends DataObject {
    static $db = array ( 'Tag' =>'Varchar');
    static $indexes = array ('Tag'=> 'unique (Tag) ' );
    static $default_records = array ( array('Tag' => 'vavavoom' ) );
    static $belongs_many_many = array (
    'Meets' => 'Meet',
    'Entries' => 'Entry'
    static function getTagByName ($name) {
    $name = trim ( strtolower($name));
    $me = DataObject::get_one('Tag',"Tag = '$name'");
    if (! $me) {
    $me = new Tag();
    $me -> Tag = $name;
    $me -> write();
    else { $me = new Tag($me->record);
    return $me;


Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.