mirror of
https://github.com/silverstripe/silverstripe-tagfield
synced 2024-10-22 11:05:32 +02:00
docs: Update readme with an example for has_one
relations.
This commit is contained in:
parent
8da9f83cde
commit
3f1d65e88e
39
readme.md
39
readme.md
@ -73,6 +73,45 @@ $field = TagField::create(
|
||||
**Note:** This assumes you have imported the namespaces class, e.g. use
|
||||
SilverStripe\TagField\TagField;
|
||||
|
||||
|
||||
#### Has-One Relations
|
||||
|
||||
You can also use the TagField to select values for `has_one` relations.
|
||||
Let's assume, that a `BlogPost` *has one* `BlogCategory`.
|
||||
|
||||
```php
|
||||
class BlogCategory extends DataObject
|
||||
{
|
||||
private static $db = [
|
||||
'Title' => 'Varchar(200)',
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\DataObject;
|
||||
|
||||
class BlogPost extends DataObject
|
||||
{
|
||||
private static $has_one = [
|
||||
'BlogCategory' => BlogCategory::class
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
```php
|
||||
$field = TagField::create(
|
||||
'BlogCategoryID',
|
||||
$this->fieldLabel('BlogCategory'),
|
||||
BlogCategory::get()
|
||||
)
|
||||
->setIsMultiple(false)
|
||||
->setCanCreate(true);
|
||||
```
|
||||
|
||||
**Note:** We're using the `ID` suffix for the field-name (eg. `BlogCategoryID` instead of `BlogCategory`) and
|
||||
only allow one value by setting `->setIsMultiple(false)`
|
||||
|
||||
### String Tags
|
||||
|
||||
```php
|
||||
|
Loading…
Reference in New Issue
Block a user