Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

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

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

secondary indices in tables


Go to End


2 Posts   1559 Views

Avatar
jahbini

Community Member, 13 Posts

28 July 2008 at 8:43pm

Edited: 28/07/2008 8:45pm

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?

Avatar
jahbini

Community Member, 13 Posts

29 July 2008 at 3:27pm

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)'

<?php

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;
        }
}

?>