From 12e26407ef2dfc4c9e3a29cd5e01cc27f5ac0084 Mon Sep 17 00:00:00 2001 From: Jonathon Menz Date: Fri, 29 May 2015 14:23:03 -0700 Subject: [PATCH] DOCS: Added default value examples --- .../00_Model/04_Data_Types_and_Casting.md | 47 +++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/docs/en/02_Developer_Guides/00_Model/04_Data_Types_and_Casting.md b/docs/en/02_Developer_Guides/00_Model/04_Data_Types_and_Casting.md index 6a31ea375..27e74ac59 100644 --- a/docs/en/02_Developer_Guides/00_Model/04_Data_Types_and_Casting.md +++ b/docs/en/02_Developer_Guides/00_Model/04_Data_Types_and_Casting.md @@ -42,8 +42,49 @@ In the `Player` example, we have four database columns each with a different dat * [api:Time]: A time field * [api:Varchar]: A variable-length string of up to 255 characters, designed to store raw text. -You can define your own [api:DBField] instances if required as well. See the API documentation for a list of all the -available subclasses. +See the [API documentation](api:DBField) for a full list of available Data Types. You can define your own [api:DBField] instances if required as well. + +## Default Values + +### Default values for new objects + +For complex default values for newly instantiated objects see [Dynamic Default Values](how_tos/dynamic_default_fields). +For simple values you can make use of the `$defaults` array. For example: + + :::php + 'Int', + 'Condition' => 'Enum(array("New","Fair","Junk"))' + ); + + private static $defaults = array( + 'Wheels' => 4, + 'Condition' => 'New' + ); + } + +### Default values for new database columns + +When adding a new `$db` field to a DataObject you can specify a default value +to be applied to all existing records when the column is added in the database +for the first time. This will also be applied to any newly created objects +going forward. You do this be passing an argument for the default value in your +`$db` items. For example: + + :::php + 'Int(4)', + 'Condition' => 'Enum(array("New","Fair","Junk"), "New")' + ); + } ## Formatting Output @@ -81,7 +122,7 @@ Then we can refer to a new `Name` column on our `Player` instances. In templates echo $player->getName()->LimitCharacters(2); // returns "Sa.." -### Casting +## Casting Rather than manually returning objects from your custom functions. You can use the `$casting` property.