silverstripe-tagfield/README.md

70 lines
1.4 KiB
Markdown
Raw Normal View History

2015-04-28 01:55:33 +02:00
# TagField Module
2015-04-14 03:15:28 +02:00
2015-05-15 12:29:17 +02:00
[![Build Status](http://img.shields.io/travis/silverstripe-labs/silverstripe-tagfield.svg?style=flat-square)](https://travis-ci.org/silverstripe-labs/silverstripe-tagfield)
[![Code Quality](http://img.shields.io/scrutinizer/g/silverstripe-labs/silverstripe-tagfield.svg?style=flat-square)](https://scrutinizer-ci.com/g/silverstripe-labs/silverstripe-tagfield)
2015-04-28 01:55:33 +02:00
## Requirements
* SilverStripe 3.1 or newer
* Database: MySQL 5+, SQLite3, Postgres 8.3, SQL Server 2008
## Download/Information
* http://silverstripe.org/tag-field-module
## Usage
2015-05-15 12:29:17 +02:00
### Relational Tags
2015-04-28 01:55:33 +02:00
```php
class BlogPost extends DataObject {
2015-09-08 04:12:28 +02:00
private static $many_many = array(
2015-04-28 01:55:33 +02:00
'BlogTags' => 'BlogTag'
);
}
```
```php
class BlogTag extends DataObject {
2015-09-08 04:12:28 +02:00
private static $db = array(
2015-04-28 01:55:33 +02:00
'Title' => 'Varchar(200)',
);
2015-09-08 04:12:28 +02:00
private static $belongs_many_many = array(
2015-04-28 01:55:33 +02:00
'BlogPosts' => 'BlogPost'
);
}
```
```php
$field = TagField::create(
'BlogTags',
'Blog Tags',
BlogTag::get(),
2015-09-08 04:12:28 +02:00
$this->BlogTags()
)
->setShouldLazyLoad(true) // tags should be lazy loaded
2015-09-08 04:12:28 +02:00
->setCanCreate(true); // new tag DataObjects can be created
2015-05-15 12:29:17 +02:00
```
### String Tags
```php
class BlogPost extends DataObject {
2015-09-08 04:12:28 +02:00
private static $db = array(
2015-05-15 12:29:17 +02:00
'Tags' => 'Text'
);
}
```
```php
$field = StringTagField::create(
2015-09-08 04:12:28 +02:00
'Tags',
'Tags',
array('one', 'two'),
explode(',', $this->Tags)
2015-05-15 12:29:17 +02:00
);
$field->setShouldLazyLoad(true); // tags should be lazy loaded
```