Skip to content

Commit

Permalink
docs: Update readme with an example for has_one relations.
Browse files Browse the repository at this point in the history
  • Loading branch information
bummzack committed Jul 28, 2022
1 parent 8da9f83 commit 3f1d65e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions readme.md
Expand Up @@ -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
Expand Down

0 comments on commit 3f1d65e

Please sign in to comment.