From 34c5cb8d1b5750c2bb521dcae14580765b3980cb Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 13 Dec 2013 12:09:12 +0100 Subject: [PATCH] Added index docs to DataObject Thanks to @oddnoc for getting this started, see https://github.com/silverstripe/silverstripe-framework/pull/2686 --- docs/en/reference/dataobject.md | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/docs/en/reference/dataobject.md b/docs/en/reference/dataobject.md index 277e4d6fa..56d5daba8 100644 --- a/docs/en/reference/dataobject.md +++ b/docs/en/reference/dataobject.md @@ -250,6 +250,53 @@ The CMS default sections as well as custom interfaces like `[ModelAdmin](/reference/modeladmin)` or `[GridField](/reference/gridfield)` already enforce these permissions. +## Indexes + +It is sometimes desirable to add indexes to your data model, whether to +optimize queries or add a uniqueness constraint to a field. This is done +through the `DataObject::$indexes` map, which maps index names to descriptor +arrays that represent each index. There's several supported notations: + + :::php + # Simple + private static $indexes = array( + '' => true + ); + + # Advanced + private static $indexes = array( + '' => array('type' => '', 'value' => '""') + ); + + # SQL + private static $indexes = array( + '' => 'unique("")' + ); + +The `` can be an an arbitrary identifier in order to allow for more than one +index on a specific database column. +The "advanced" notation supports more `` notations. +These vary between database drivers, but all of them support the following: + + * `index`: Standard index + * `unique`: Index plus uniqueness constraint on the value + * `fulltext`: Fulltext content index + +In order to use more database specific or complex index notations, +we also support raw SQL for as a value in the `$indexes` definition. +Keep in mind this will likely make your code less portable between databases. + +Example: A combined index on a two fields. + + :::php + private static $db = array( + 'MyField' => 'Varchar', + 'MyOtherField' => 'Varchar', + ); + private static $indexes = array( + 'MyIndexName' => array('type' => 'index', 'value' => '"MyField","MyOtherField"'), + ); + ## API Documentation `[api:DataObject]`