Skip to content

Commit

Permalink
DOC Update cascade_deletes information for has_many relationships
Browse files Browse the repository at this point in the history
This is a workaround until silverstripe/silverstripe-framework#9612 is resolved.
  • Loading branch information
edwilde authored May 29, 2024
1 parent e36a8ce commit 995af58
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions en/02_Developer_Guides/00_Model/02_Relations.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,9 +989,45 @@ class ParentObject 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.
In this example, when the parent object is deleted, the child specified by the `has_one` relation will also be deleted. The relation types `has_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.
### Cascading deletions with `has_many`
When the parent object is deleted, the end child specified by the `cascade_deletes` will also be removed, not unlinked. To remove the link between the two objects, use `has_many` on the relation object directly to create an alternate view of the relationship which can be unlinked.
For example:
```php
namespace App\Model;
use SilverStripe\ORM\DataObject;
class Team extends DataObject
{
// Define the many-to-many relationship using a custom relation table
private static array $many_many = [
'Supporters' => [
'through' => TeamSupporter::class,
'from' => 'Team',
'to' => 'Supporter',
],
];
// Add a different perspective of the relationship data
private static array $has_many = [
'SupportersRelation' => TeamSupporter::class . '.Team',
];
// Unlink the relationship when the parent object is deleted
private static array $cascade_duplicates = [
'SupportersRelation',
];
// ...
}
```
### Cascading deletions with versions
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.
Expand Down

0 comments on commit 995af58

Please sign in to comment.