Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC Update cascade_deletes information for has_many relationships #523

Merged
merged 6 commits into from
May 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading