mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
$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:
parent
99d43d2211
commit
b9922a7314
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user