2019-11-18 05:58:33 +01:00
---
2014-10-27 04:40:02 +01:00
title: Relations between Records
2014-10-28 04:45:46 +01:00
summary: Relate models together using the ORM using has_one, has_many, and many_many.
2019-11-18 05:58:33 +01:00
icon: link
---
2014-10-27 04:40:02 +01:00
# Relations between Records
2017-07-03 03:22:12 +02:00
In most situations you will likely see more than one [DataObject ](api:SilverStripe\ORM\DataObject ) and several classes in your data model may relate
2014-10-27 04:40:02 +01:00
to one another. An example of this is a `Player` object may have a relationship to one or more `Team` or `Coach` classes
and could take part in many `Games` . Relations are a key part of designing and building a good data model.
Relations are built through static array definitions on a class, in the format `<relationship-name> => <classname>` .
SilverStripe supports a number of relationship types and each relationship type can have any number of relations.
## has_one
2018-08-18 15:06:08 +02:00
Many-to-1 and 1-to-1 relationships create a database-column called "`< relationship-name > `ID", in the example below this would be "TeamID" on the "Player"-table.
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2014-10-27 04:40:02 +01:00
2018-08-18 15:06:08 +02:00
class Player extends DataObject
{
private static $has_one = [
"Team" => "Team",
];
}
2018-04-19 00:53:36 +02:00
class Team extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $db = [
'Title' => 'Varchar'
];
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
private static $has_many = [
2019-11-26 03:57:45 +01:00
'Players' => Player::class,
2017-10-27 04:38:27 +02:00
];
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
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.
2018-04-23 15:33:07 +02:00
To create a has_one/has_many relationship to core classes (File, Image, etc), reference the Classname::class, like below.
```php
2018-04-24 10:49:45 +02:00
use SilverStripe\ORM\DataObject;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\File;
class Team extends DataObject
{
2018-04-23 15:33:07 +02:00
private static $has_many = [
'Teamphoto' => Image::class,
'Lineup' => File::class
2018-04-24 10:49:45 +02:00
];
}
2018-04-23 15:33:07 +02:00
```
2014-10-28 04:45:46 +01:00
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()` )
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$player = Player::get()->byId(1);
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
$team = $player->Team();
// returns a 'Team' instance.
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
echo $player->Team()->Title;
// returns the 'Title' column on the 'Team' or `getTitle` if it exists.
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
The relationship can also be navigated in [templates ](../templates ).
2017-08-03 02:51:32 +02:00
2017-10-27 04:38:27 +02:00
```ss
< % with $Player %>
< % if $Team %>
Plays for $Team.Title
< % end_if %>
< % end_with %>
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
2016-01-21 23:45:00 +01:00
## Polymorphic has_one
A has_one can also be polymorphic, which allows any type of object to be associated.
This is useful where there could be many use cases for a particular data structure.
An additional column is created called "`< relationship-name > `Class", which along
with the ID column identifies the object.
2018-01-08 02:10:11 +01:00
To specify that a has_one relation is polymorphic set the type to [api:SilverStripe\ORM\DataObject]
2016-01-21 23:45:00 +01:00
Ideally, the associated has_many (or belongs_to) should be specified with dot notation.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-07 05:11:17 +02:00
2018-04-19 00:53:36 +02:00
class Player extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $has_many = [
2019-11-26 03:57:45 +01:00
"Fans" => Fan::class.".FanOf",
2017-10-27 04:38:27 +02:00
];
}
2018-04-19 00:53:36 +02:00
class Team extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $has_many = [
2019-11-26 03:57:45 +01:00
"Fans" => Fan::class.".FanOf",
2017-10-27 04:38:27 +02:00
];
}
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
// Type of object returned by $fan->FanOf() will vary
2018-04-19 00:53:36 +02:00
class Fan extends DataObject
2017-10-27 04:38:27 +02:00
{
2017-08-03 05:35:09 +02:00
2017-10-27 04:38:27 +02:00
// Generates columns FanOfID and FanOfClass
private static $has_one = [
2018-01-08 02:10:11 +01:00
"FanOf" => DataObject::class,
2017-10-27 04:38:27 +02:00
];
}
2017-08-03 02:51:32 +02:00
```
2016-01-21 23:45:00 +01:00
2019-11-18 05:58:33 +01:00
[warning]
2016-01-21 23:45:00 +01:00
Note: The use of polymorphic relationships can affect query performance, especially
on joins, and also increases the complexity of the database and necessary user code.
They should be used sparingly, and only where additional complexity would otherwise
be necessary. E.g. Additional parent classes for each respective relationship, or
duplication of code.
2019-11-18 05:58:33 +01:00
[/warning]
2016-01-21 23:45:00 +01:00
2014-10-27 04:40:02 +01:00
## has_many
2014-10-28 04:45:46 +01:00
Defines 1-to-many joins. As you can see from the previous example, `$has_many` goes hand in hand with `$has_one` .
2014-10-27 04:40:02 +01:00
2019-11-18 05:58:33 +01:00
[alert]
2014-10-27 04:40:02 +01:00
Please specify a $has_one-relationship on the related child-class as well, in order to have the necessary accessors
2018-04-23 15:33:07 +02:00
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
```
2019-11-18 05:58:33 +01:00
[/alert]
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2014-10-27 04:40:02 +01:00
2018-04-19 00:53:36 +02:00
class Team extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $db = [
2019-11-26 03:57:45 +01:00
'Title' => 'Varchar',
2017-10-27 04:38:27 +02:00
];
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
private static $has_many = [
2019-11-26 03:57:45 +01:00
'Players' => Player::class,
2017-10-27 04:38:27 +02:00
];
}
2018-04-19 00:53:36 +02:00
class Player extends DataObject
2017-10-27 04:38:27 +02:00
{
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
private static $has_one = [
2019-11-26 03:57:45 +01:00
"Team" => Team::class,
2017-10-27 04:38:27 +02:00
];
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
Much like the `has_one` relationship, `has_many` can be navigated through the `ORM` as well. The only difference being
2017-07-03 03:22:12 +02:00
you will get an instance of [HasManyList ](api:SilverStripe\ORM\HasManyList ) rather than the object.
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$team = Team::get()->first();
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
echo $team->Players();
// [HasManyList]
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
echo $team->Players()->Count();
// returns '14';
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
foreach($team->Players() as $player) {
echo $player->FirstName;
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
2016-07-07 23:32:43 +02:00
To specify multiple `$has_many` to the same object you can use dot notation to distinguish them like below:
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-03 05:35:09 +02:00
2018-04-19 00:53:36 +02:00
class Person extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $has_many = [
2019-11-26 03:57:45 +01:00
"Managing" => Company::class.".Manager",
"Cleaning" => Company::class.".Cleaner",
2017-10-27 04:38:27 +02:00
];
}
2018-04-19 00:53:36 +02:00
class Company extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $has_one = [
2019-11-26 03:57:45 +01:00
"Manager" => Person::class,
"Cleaner" => Person::class,
2017-10-27 04:38:27 +02:00
];
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
Multiple `$has_one` relationships are okay if they aren't linking to the same object type. Otherwise, they have to be
named.
2016-07-07 23:32:43 +02:00
If you're using the default scaffolded form fields with multiple `has_one` relationships, you will end up with a CMS field for each relation. If you don't want these you can remove them by their IDs:
2017-08-07 05:11:17 +02:00
```php
2017-10-27 04:38:27 +02:00
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->removeByName(array('ManagerID', 'CleanerID'));
return $fields;
}
2017-08-07 05:11:17 +02:00
```
2014-10-27 04:40:02 +01:00
## belongs_to
2018-04-19 00:53:36 +02:00
Defines a 1-to-1 relationship with another object, which declares the other end of the relationship with a
corresponding `$has_one` . A single database column named `<relationship-name>ID` will be created in the object with the
`$has_one` , but the $belongs_to by itself will not create a database field. This field will hold the ID of the object
2014-10-27 04:40:02 +01:00
declaring the `$belongs_to` .
2018-04-19 00:53:36 +02:00
Similarly with `$has_many` , dot notation can be used to explicitly specify the `$has_one` which refers to this relation.
2014-10-27 04:40:02 +01:00
This is not mandatory unless the relationship would be otherwise ambiguous.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-03 05:35:09 +02:00
2018-04-19 00:53:36 +02:00
class Team extends DataObject
2017-10-27 04:38:27 +02:00
{
2018-04-19 00:53:36 +02:00
2017-10-27 04:38:27 +02:00
private static $has_one = [
2019-11-26 03:57:45 +01:00
'Coach' => Coach::class
2017-10-27 04:38:27 +02:00
];
}
2018-04-19 00:53:36 +02:00
class Coach extends DataObject
2017-10-27 04:38:27 +02:00
{
2018-04-19 00:53:36 +02:00
2017-10-27 04:38:27 +02:00
private static $belongs_to = [
2019-11-26 03:57:45 +01:00
'Team' => Team::class.'.Coach'
2017-10-27 04:38:27 +02:00
];
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## many_many
2016-09-26 07:22:19 +02:00
Defines many-to-many joins, which uses a third table created between the two to join pairs.
There are two ways in which this can be declared, which are described below, depending on
how the developer wishes to manage this join table.
2014-10-27 04:40:02 +01:00
2019-11-18 05:58:33 +01:00
[warning]
2016-09-26 07:22:19 +02:00
Please specify a $belongs_many_many-relationship on the related class as well, in order
to have the necessary accessors available on both ends.
2019-11-18 05:58:33 +01:00
[/warning]
2014-10-27 04:40:02 +01:00
2016-09-26 07:22:19 +02:00
Much like the `has_one` relationship, `many_many` can be navigated through the `ORM` as well.
2017-07-03 03:22:12 +02:00
The only difference being you will get an instance of [ManyManyList ](api:SilverStripe\ORM\ManyManyList ) or
[ManyManyThroughList ](api:SilverStripe\ORM\ManyManyThroughList ) rather than the object.
2016-09-26 07:22:19 +02:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$team = Team::get()->byId(1);
2016-09-26 07:22:19 +02:00
2017-10-27 04:38:27 +02:00
$supporters = $team->Supporters();
// returns a 'ManyManyList' instance.
2017-08-03 02:51:32 +02:00
```
2016-09-26 07:22:19 +02:00
### Automatic many_many table
If you specify only a single class as the other side of the many-many relationship, then a
table will be automatically created between the two (this-class)_(relationship-name), will
be created with a pair of ID fields.
Extra fields on the mapping table can be created by declaring a `many_many_extraFields`
config to add extra columns.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-07 05:11:17 +02:00
2018-04-19 00:53:36 +02:00
class Team extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $many_many = [
2019-11-26 03:57:45 +01:00
"Supporters" => Supporter::class,
2017-10-27 04:38:27 +02:00
];
private static $many_many_extraFields = [
2017-08-07 05:11:17 +02:00
'Supporters' => [
2018-04-19 00:53:36 +02:00
'Ranking' => 'Int'
2017-08-07 05:11:17 +02:00
]
2017-10-27 04:38:27 +02:00
];
}
2017-08-07 05:11:17 +02:00
2018-04-19 00:53:36 +02:00
class Supporter extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $belongs_many_many = [
2019-11-26 03:57:45 +01:00
"Supports" => Team::class,
2017-10-27 04:38:27 +02:00
];
}
2017-08-03 02:51:32 +02:00
```
2016-09-26 07:22:19 +02:00
2018-11-01 01:42:27 +01:00
To ensure this `many_many` is sorted by "Ranking" by default you can add this to your config:
```yaml
Team_Supporters:
default_sort: '"Team_Supporter"."Ranking" ASC'
```
`Team_Supporters` is the table name automatically generated for the many_many relation in this case.
2016-09-26 07:22:19 +02:00
### many_many through relationship joined on a separate DataObject
If necessary, a third DataObject class can instead be specified as the joining table,
rather than having the ORM generate an automatically scaffolded table. This has the following
advantages:
- Allows versioning of the mapping table, including support for the
[ownership api ](/developer_guides/model/versioning ).
- Allows support of other extensions on the mapping table (e.g. subsites).
- Extra fields can be managed separately to the joined dataobject, even via a separate
GridField or form.
This is declared via array syntax, with the following keys on the many_many:
- `through` Class name of the mapping table
- `from` Name of the has_one relationship pointing back at the object declaring many_many
- `to` Name of the has_one relationship pointing to the object declaring belongs_many_many.
2018-04-19 00:53:36 +02:00
2018-11-01 01:42:27 +01:00
Just like a any normal DataObject, you can apply a default sort which will be applied when
accessing many many through relations.
2016-10-03 03:51:34 +02:00
Note: The `through` class must not also be the name of any field or relation on the parent
or child record.
2016-09-26 07:22:19 +02:00
The syntax for `belongs_many_many` is unchanged.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-07 05:11:17 +02:00
2018-04-19 00:53:36 +02:00
class Team extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $many_many = [
2017-08-07 05:11:17 +02:00
"Supporters" => [
2018-07-27 10:30:13 +02:00
'through' => TeamSupporter::class,
2017-10-27 04:38:27 +02:00
'from' => 'Team',
'to' => 'Supporter',
2017-08-07 05:11:17 +02:00
]
2017-10-27 04:38:27 +02:00
];
}
2018-04-19 00:53:36 +02:00
class Supporter extends DataObject
2017-10-27 04:38:27 +02:00
{
2018-07-27 10:30:13 +02:00
// Prior to 4.2.0, this also needs to include the reverse relation name via dot-notation
// i.e. 'Supports' => Team::class . '.Supporters'
2017-10-27 04:38:27 +02:00
private static $belongs_many_many = [
2018-07-27 10:30:13 +02:00
'Supports' => Team::class,
2017-10-27 04:38:27 +02:00
];
}
2018-04-19 00:53:36 +02:00
class TeamSupporter extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $db = [
2017-08-07 05:11:17 +02:00
'Ranking' => 'Int',
2017-10-27 04:38:27 +02:00
];
2018-04-19 00:53:36 +02:00
2017-10-27 04:38:27 +02:00
private static $has_one = [
2018-07-27 10:30:13 +02:00
'Team' => Team::class,
'Supporter' => Supporter::class,
2017-10-27 04:38:27 +02:00
];
2018-11-01 01:42:27 +01:00
private static $default_sort = '"TeamSupporter"."Ranking" ASC'
2017-10-27 04:38:27 +02:00
}
2017-08-03 02:51:32 +02:00
```
2016-09-26 07:22:19 +02:00
2016-10-03 03:51:34 +02:00
In order to filter on the join table during queries, you can use the class name of the joining table
2016-09-26 07:22:19 +02:00
for any sql conditions.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$team = Team::get()->byId(1);
$supporters = $team->Supporters()->where(['"TeamSupporter"."Ranking"' => 1]);
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
2016-10-03 03:51:34 +02:00
Note: ->filter() currently does not support joined fields natively due to the fact that the
query for the join table is isolated from the outer query controlled by DataList.
2018-03-21 22:26:25 +01:00
### Polymorphic many_many (Experimental)
Using many_many through, it is possible to support polymorphic relations on the mapping table.
Note, that this feature is currently experimental, and has certain limitations:
- This feature only works with many_many through
- This feature will only allow polymorphic many_many, but not belongs_many_many. However,
you can have a has_many relation to the mapping table on this side, and iterate through this
to collate parent records.
For instance, this is how you would link an arbitrary object to many_many tags.
```php
use SilverStripe\ORM\DataObject;
2018-04-19 00:53:36 +02:00
class SomeObject extends DataObject
2018-03-21 22:26:25 +01:00
{
// This same many_many may also exist on other classes
private static $many_many = [
"Tags" => [
'through' => TagMapping::class,
'from' => 'Parent',
'to' => 'Tag',
]
];
}
2018-04-19 00:53:36 +02:00
class Tag extends DataObject
2018-03-21 22:26:25 +01:00
{
// has_many works, but belongs_many_many will not
private static $has_many = [
'TagMappings' => TagMapping::class,
];
2018-04-19 00:53:36 +02:00
2018-03-21 22:26:25 +01:00
/**
* Example iterator placeholder for belongs_many_many.
* This is a list of arbitrary types of objects
* @return Generator|DataObject[]
*/
public function TaggedObjects()
{
foreach ($this->TagMappings() as $mapping) {
yield $mapping->Parent();
}
}
2018-04-19 00:53:36 +02:00
2018-03-21 22:26:25 +01:00
}
2018-04-19 00:53:36 +02:00
class TagMapping extends DataObject
2018-03-21 22:26:25 +01:00
{
private static $has_one = [
'Parent' => DataObject::class, // Polymorphic has_one
'Tag' => Tag::class,
];
}
```
2016-09-26 07:22:19 +02:00
### Using many_many in templates
2014-10-27 04:40:02 +01:00
The relationship can also be navigated in [templates ](../templates ).
2016-10-03 03:51:34 +02:00
The joined record can be accessed via `Join` or `TeamSupporter` property (many_many through only)
2017-08-03 02:51:32 +02:00
2017-10-27 04:38:27 +02:00
```ss
< % with $Supporter %>
< % loop $Supports %>
Supports $Title < % if $TeamSupporter %>(rank $TeamSupporter.Ranking)< % end_if %>
< % end_if %>
< % end_with %>
2017-08-03 02:51:32 +02:00
```
2016-10-03 03:51:34 +02:00
You can also use `$Join` in place of the join class alias (`$TeamSupporter`), if your template
2018-04-19 00:53:36 +02:00
is class-agnostic and doesn't know the type of the join table.
2016-10-03 03:51:34 +02:00
2016-09-26 07:22:19 +02:00
## belongs_many_many
The belongs_many_many represents the other side of the relationship on the target data class.
When using either a basic many_many or a many_many through, the syntax for belongs_many_many is the same.
To specify multiple $many_manys between the same classes, specify use the dot notation to
distinguish them like below:
2015-01-24 00:13:17 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-07 05:11:17 +02:00
2018-04-19 00:53:36 +02:00
class Category extends DataObject
2017-10-27 04:38:27 +02:00
{
2018-04-19 00:53:36 +02:00
2017-10-27 04:38:27 +02:00
private static $many_many = [
2019-11-26 03:57:45 +01:00
'Products' => Product::class,
'FeaturedProducts' => Product::class,
2017-10-27 04:38:27 +02:00
];
}
2017-08-03 05:35:09 +02:00
2018-04-19 00:53:36 +02:00
class Product extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $belongs_many_many = [
2019-11-26 03:57:45 +01:00
'Categories' => Category::class.'.Products',
'FeaturedInCategories' => Category::class.'.FeaturedProducts',
2017-10-27 04:38:27 +02:00
];
}
2017-08-03 02:51:32 +02:00
```
2014-10-28 04:45:46 +01:00
2016-09-26 07:22:19 +02:00
If you're unsure about whether an object should take on `many_many` or `belongs_many_many` ,
the best way to think about it is that the object where the relationship will be edited
(i.e. via checkboxes) should contain the `many_many` . For instance, in a `many_many` of
Product => Categories, the `Product` should contain the `many_many` , because it is much
more likely that the user will select Categories for a Product than vice-versa.
2014-10-28 04:45:46 +01:00
2018-11-01 01:42:27 +01:00
2017-08-08 06:46:34 +02:00
## Cascading deletions
Relationships between objects can cause cascading deletions, if necessary, through configuration of the
`cascade_deletes` config on the parent class.
```php
2017-10-26 02:22:02 +02:00
use SilverStripe\ORM\DataObject;
2017-08-08 06:46:34 +02:00
class ParentObject extends DataObject {
private static $has_one = [
'Child' => ChildObject::class,
];
private static $cascade_deletes = [
'Child',
];
}
class ChildObject extends DataObject {
}
```
In this example, when the Parent object is deleted, the Child specified by the has_one relation will also
be deleted. Note that all relation types (has_many, many_many, belongs_many_many, belongs_to, and has_one)
are supported, as are methods that return lists of objects but do not correspond to a physical database relation.
If your object is versioned, cascade_deletes will also act as "cascade unpublish", such that any unpublish
on a parent object will trigger unpublish on the child, similarly to how `owns` causes triggered publishing.
2018-04-19 00:53:36 +02:00
See the [versioning docs ](/developer_guides/model/versioning ) for more information on ownership.
2017-08-08 06:46:34 +02:00
2019-11-18 05:58:33 +01:00
[alert]
2019-06-18 06:27:29 +02:00
Declaring cascade_deletes implies delete permissions on the listed objects.
Built-in controllers using delete operations check canDelete() on the owner, but not on the owned object.
2019-11-18 05:58:33 +01:00
[/alert]
2019-06-18 06:27:29 +02:00
2018-01-30 06:28:28 +01:00
## Cascading duplications
Similar to `cascade_deletes` there is also a `cascade_duplicates` config which works in much the same way.
When you invoke `$dataObject->duplicate()` , relation names specified by this config will be duplicated
and saved against the new clone object.
Note that duplications will act differently depending on the kind of relation:
- Exclusive relationships (e.g. has_many, belongs_to) will be explicitly duplicated.
- Non-exclusive many_many will not be duplicated, but the mapping table values will instead
be copied for this record.
- Non-exclusive has_one relationships are not normally necessary to duplicate, since both parent and clone
can normally share the same relation ID. However, if this is declared in `cascade_duplicates` any
has one will be similarly duplicated as though it were an exclusive relationship.
For example:
```php
use SilverStripe\ORM\DataObject;
class ParentObject extends DataObject {
private static $many_many = [
'RelatedChildren' => ChildObject::class,
];
private static $cascade_duplicates = [ 'RelatedChildren' ];
}
class ChildObject extends DataObject {
}
```
When duplicating objects you can disable recursive duplication by passing in `false` to the second
argument of duplicate().
E.g.
```php
$parent = ParentObject::get()->first();
$dupe = $parent->duplicate(true, false);
```
2014-10-27 04:40:02 +01:00
## Adding relations
2018-04-19 00:53:36 +02:00
Adding new items to a relations works the same, regardless if you're editing a **has_many** or a **many_many** . They are
2017-07-03 03:22:12 +02:00
encapsulated by [HasManyList ](api:SilverStripe\ORM\HasManyList ) and [ManyManyList ](api:SilverStripe\ORM\ManyManyList ), both of which provide very similar APIs, e.g. an `add()`
2014-10-27 04:40:02 +01:00
and `remove()` method.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$team = Team::get()->byId(1);
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
// create a new supporter
$supporter = new Supporter();
$supporter->Name = "Foo";
$supporter->write();
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
// add the supporter.
$team->Supporters()->add($supporter);
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## Custom Relations
2018-04-19 00:53:36 +02:00
You can use the ORM to get a filtered result list without writing any SQL. For example, this snippet gets you the
2014-10-27 04:40:02 +01:00
"Players"-relation on a team, but only containing active players.
2017-07-03 03:22:12 +02:00
See [DataObject::$has_many ](api:SilverStripe\ORM\DataObject::$has_many ) for more info on the described relations.
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2014-10-27 04:40:02 +01:00
2018-04-19 00:53:36 +02:00
class Team extends DataObject
2017-10-27 04:38:27 +02:00
{
private static $has_many = [
2019-11-26 03:57:45 +01:00
"Players" => Player::class
2017-10-27 04:38:27 +02:00
];
2018-04-19 00:53:36 +02:00
public function ActivePlayers()
2017-10-27 04:38:27 +02:00
{
return $this->Players()->filter('Status', 'Active');
2017-08-07 05:11:17 +02:00
}
2017-10-27 04:38:27 +02:00
}
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
2019-11-18 05:58:33 +01:00
[notice]
2018-04-19 00:53:36 +02:00
Adding new records to a filtered `RelationList` like in the example above doesn't automatically set the filtered
2014-10-27 04:40:02 +01:00
criteria on the added record.
2019-11-18 05:58:33 +01:00
[/notice]
2014-10-27 04:40:02 +01:00
## Relations on Unsaved Objects
2018-04-19 00:53:36 +02:00
You can also set *has_many* and *many_many* relations before the `DataObject` is saved. This behavior uses the
[UnsavedRelationList ](api:SilverStripe\ORM\UnsavedRelationList ) and converts it into the correct `RelationList` when saving the `DataObject` for the first
2014-10-27 04:40:02 +01:00
time.
This unsaved lists will also recursively save any unsaved objects that they contain.
2018-04-19 00:53:36 +02:00
As these lists are not backed by the database, most of the filtering methods on `DataList` cannot be used on a list of
this type. As such, an `UnsavedRelationList` should only be used for setting a relation before saving an object, not
2014-10-27 04:40:02 +01:00
for displaying the objects contained in the relation.
2018-08-31 05:02:18 +02:00
## Link Tracking
You can control the visibility of the `Link Tracking` tab by setting the `show_sitetree_link_tracking` config.
This defaults to `false` for most `DataObject` 's.
It is also possible to control the visibility of the `File Tracking` tab by setting the `show_file_link_tracking` config.
2017-11-27 04:39:17 +01:00
## Related Lessons
* [Working with data relationships -- has_many ](https://www.silverstripe.org/learn/lessons/v4/working-with-data-relationships-has-many-1 )
* [Working with data relationships -- many_many ](https://www.silverstripe.org/learn/lessons/v4/working-with-data-relationships-many-many-1 )
2014-10-27 04:40:02 +01:00
## Related Documentation
* [Introduction to the Data Model and ORM ](data_model_and_orm )
* [Lists ](lists )
## API Documentation
2017-07-03 03:22:12 +02:00
* [HasManyList ](api:SilverStripe\ORM\HasManyList )
* [ManyManyList ](api:SilverStripe\ORM\ManyManyList )
* [DataObject ](api:SilverStripe\ORM\DataObject )
2018-08-31 05:02:18 +02:00
* [LinkTracking ](api:SilverStripe\CMS\Model\SiteTreeLinkTracking )