$has_one docs

The ` $has_one` can be used both for `1-to-1` and `many-to-1` relations, depending on how is configured the inverse mapping on the related class. The documentation seems to suggest that `$has_one` implies a `1-to-1` relation, but then it gives an example of a `many-to-1` relationship. Since we are focusing on `$has_one` I would also put the `Player` class before the `Team` class.
This commit is contained in:
maks 2018-08-18 15:06:08 +02:00 committed by GitHub
parent 99d43d2211
commit b9922a7314
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 8 deletions

View File

@ -12,12 +12,18 @@ SilverStripe supports a number of relationship types and each relationship type
## has_one
A 1-to-1 relation creates a database-column called "`<relationship-name>`ID", in the example below this would be
"TeamID" on the "Player"-table.
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.
```php
use SilverStripe\ORM\DataObject;
class Player extends DataObject
{
private static $has_one = [
"Team" => "Team",
];
}
class Team extends DataObject
{
private static $db = [
@ -28,12 +34,6 @@ class Team extends DataObject
'Players' => 'Player'
];
}
class Player extends DataObject
{
private static $has_one = [
"Team" => "Team",
];
}
```
This defines a relationship called `Team` which links to a `Team` class. The `ORM` handles navigating the relationship