From 8b36a2ceae85d8aa3eafe30fe404cbf4b90dc546 Mon Sep 17 00:00:00 2001 From: blueskies79 Date: Mon, 23 Apr 2018 15:33:07 +0200 Subject: [PATCH 1/2] Add documentation on relating to core classes Adding a relationship to core classes brings some extra syntax issues. I think it would be usefull to not that core classes are realted through Image::class and that a has-one-relationship needed on a core class when relating them thru has_many, can be set up through yml. --- .../02_Developer_Guides/00_Model/02_Relations.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/en/02_Developer_Guides/00_Model/02_Relations.md b/docs/en/02_Developer_Guides/00_Model/02_Relations.md index f2d795b60..0a13602d7 100644 --- a/docs/en/02_Developer_Guides/00_Model/02_Relations.md +++ b/docs/en/02_Developer_Guides/00_Model/02_Relations.md @@ -39,6 +39,15 @@ class Player extends DataObject This defines a relationship called `Team` which links to a `Team` class. The `ORM` handles navigating the relationship and provides a short syntax for accessing the related object. +To create a has_one/has_many relationship to core classes (File, Image, etc), reference the Classname::class, like below. + +```php + private static $has_many = [ + 'Teamphoto' => Image::class, + 'Lineup' => File::class + ]; +``` + At the database level, the `has_one` creates a `TeamID` field on `Player`. A `has_many` field does not impose any database changes. It merely injects a new method into the class to access the related records (in this case, `Players()`) ```php @@ -113,7 +122,12 @@ Defines 1-to-many joins. As you can see from the previous example, `$has_many` g
Please specify a $has_one-relationship on the related child-class as well, in order to have the necessary accessors -available on both ends. +available on both ends. To add a $has_one-relationship on core classes, yml config settings can be used: +```yml +SilverStripe\Assets\Image: + has_one: + MyDataObject: MyDataObject +```
```php From 84ae5ba321e344bfb7cb8b699fe43b296c2e7c2d Mon Sep 17 00:00:00 2001 From: blueskies79 Date: Tue, 24 Apr 2018 10:49:45 +0200 Subject: [PATCH 2/2] Added FQCN for Image and File in use statements Sorry, first pull request. I added the Image and File use statements as requested. And surrounded the has_many example with the class name so it makes more sense. --- docs/en/02_Developer_Guides/00_Model/02_Relations.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/en/02_Developer_Guides/00_Model/02_Relations.md b/docs/en/02_Developer_Guides/00_Model/02_Relations.md index 0a13602d7..d33c3276e 100644 --- a/docs/en/02_Developer_Guides/00_Model/02_Relations.md +++ b/docs/en/02_Developer_Guides/00_Model/02_Relations.md @@ -42,10 +42,17 @@ and provides a short syntax for accessing the related object. To create a has_one/has_many relationship to core classes (File, Image, etc), reference the Classname::class, like below. ```php +use SilverStripe\ORM\DataObject; +use SilverStripe\Assets\Image; +use SilverStripe\Assets\File; + +class Team extends DataObject +{ private static $has_many = [ 'Teamphoto' => Image::class, 'Lineup' => File::class - ]; + ]; +} ``` At the database level, the `has_one` creates a `TeamID` field on `Player`. A `has_many` field does not impose any database changes. It merely injects a new method into the class to access the related records (in this case, `Players()`)