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.

Archive

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

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

secondary indices in tables


Reply

2 Posts   1283 Views

Avatar
jahbini

28 July 2008 at 8:43pm (Last edited: 28 July 2008 8:45pm), Community Member, 13 Posts

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

29 July 2008 at 3:27pm Community Member, 13 Posts

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

?>