Login | Forgot password | Register
What is OpenID?
OpenID is an Internet-wide identity system that allows you to sign in to many websites with a single account.
With OpenID, your ID becomes a URL (e.g. http://username.myopenid.com/). You can get a free OpenID for example from myopenid.com.
For more information visit the official OpenID site.
Archive
SilverStripe Forums » Archive » secondary indices in tables
Our old forums are still available as a read-only archive.
|
Page:
1
|
Go to End | |
| Author | Topic: secondary indices in tables | 647 Views |
-
secondary indices in tables

28 July 2008 at 8:43pm Last edited: 28 July 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?
-
Re: secondary indices in tables

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;
}
}?>
| 647 Views | ||
|
Page:
1
|
Go to Top |

