Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » SOLVED: Index names

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

Page: 1
Go to End
Author Topic: 2176 Views
  • Vromepiet
    Avatar
    Community Member
    11 Posts

    SOLVED: Index names Link to this post

    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.

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: SOLVED: Index names Link to this post

    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.

  • Vromepiet
    Avatar
    Community Member
    11 Posts

    Re: SOLVED: Index names Link to this post

    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.

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: SOLVED: Index names Link to this post

    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.

  • Vromepiet
    Avatar
    Community Member
    11 Posts

    Re: SOLVED: Index names Link to this post

    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.

    2176 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.