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.

Customising the CMS /

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

SOLVED: Index names


Go to End


5 Posts   4460 Views

Avatar
Vromepiet

Community Member, 11 Posts

19 July 2010 at 6:54am

Edited: 20/07/2010 7:05am

Hello,

Simple question: I have added a certain index to a dataobject using the standard spec to make it unique on multiple columns. However, I cannot find how to give the index a name, it seems to default to a number.

   static $indexes = array(
      array( 'type' => 'unique', 'value' => 'Iso3166,Locale' )
   );

Can anyone help?

Regards, Jeroen.

Avatar
dhensby

Community Member, 253 Posts

19 July 2010 at 7:33am

You have to define the colums in your db static array swell. If you want to do this with a has_one or other relationship type, you have to append 'ID' to the end of the column name in the index array.

Eg:

Static $has_one = array(
'Picture' => 'Image'
);

static $indexes = array(
'PictureID' => 'unique'
);

Although, I have never assigned an index to the relationship ID before, silverstripe automatically makes it a db index.

Avatar
Vromepiet

Community Member, 11 Posts

19 July 2010 at 8:21am

Hi Pigeon,

Thanks for your answer, but I think I don't understand what you mean.
Let me explain a bit further, my code is below:

class Country extends DataObject {
	
   static $db = array(
      'Name' => 'Varchar(100)',
      'Description' => 'Varchar(200)',
      'Iso3166' => 'Varchar(10)',
   );
   
   static $indexes = array(
      array( 'type' => 'unique', 'value' => 'Iso3166,Locale' )
   );
   
   static $extensions = array(
      'Translatable',
   );
   
   // some more
 
}

The index is being created without any problem, but is does have a name "0".
How should I specify the index to give it a name?

Thanksand hopefully you have some time left to help again :-)

Jeroen.

Avatar
dhensby

Community Member, 253 Posts

19 July 2010 at 10:20pm

As far as i am aware, indexes work like this in Silverstripe:

static $db = array( 
'Name' => 'Varchar(100)', 
'Description' => 'Varchar(200)', 
'Iso3166' => 'Varchar(10)',
'Type' => 'Int'
);

static $indexes = array(
'Name' => true, //just make in an index for quick lookups
'Type' => 'Unique', //make it a unique index
'Description' => 'FullText' //full text index for the field for searching
);

Any field you want to make an index has to be defined in $db - Although theorectically you could also use a field that had been defined via a $has_one / $has_many / etc relationship.

I havent seen your indexes notation (nested arrays) to assign indexes before, but that doesn't mean it isn't right.

Avatar
Vromepiet

Community Member, 11 Posts

20 July 2010 at 7:04am

Hello,

Solved. Code below creates a named unique index on both columns.

   static $db = array(
      'Name' => 'Varchar(100)',
      'Description' => 'Varchar(200)',
      'Iso3166' => 'Varchar(10)',
   );
   
   static $indexes = array(
      'IDX_Country_ISO' => array ( 'type' => 'unique', 'value' => 'Iso3166,Locale' )
   );

Thanks pigeon for your help.

Jeroen.