Blog » Adding a database index to a SilverStripe DataObject
If you have large amounts of data, you should be using database indexes. This is how you add a database index to a DataObject in Silverstripe.
There is a static variable called $indexes The way to use it is to pass an array to indexes using array('index name' => 'fields to index')
Here is a quick example.
static $indexes = array( 'MyIndex' => '(Name)' );
To index more than 1 field, you can use the following
static $indexes = array( 'MyIndex' => '(Name,ShortDescription)' );
To use a unique index as you would on a URLSegment field, you need to pass array('Field name to index' => true) to the $indexes variable.
static $indexes = array( 'URLSegment' => true );
Dont forgot to dev/build?flush=1 your database.
Elliot says:
It's rather an easy way to add indexes on any Silverstripe dataobjects. I'm thinking that even the core dataobjects like SiteTree can be changed without "hacking the core".
Posted by Elliot, 22 February 2012 (3 months ago)
RSS feed for comments on this page | RSS feed for all comments