https://github.com/raksul/doctrinesettypebundle
The DoctrineSetTypeBundle provides MySQL SET type support for Doctrine in your Symfony application.
https://github.com/raksul/doctrinesettypebundle
Last synced: 2 months ago
JSON representation
The DoctrineSetTypeBundle provides MySQL SET type support for Doctrine in your Symfony application.
- Host: GitHub
- URL: https://github.com/raksul/doctrinesettypebundle
- Owner: raksul
- License: mit
- Created: 2015-05-01T10:33:50.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2023-07-03T08:05:35.000Z (almost 2 years ago)
- Last Synced: 2025-03-24T17:52:51.718Z (3 months ago)
- Language: PHP
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 105
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
DoctrineSetTypeBundle
=====================The `DoctrineSetTypeBundle` provides support MySQL SET type for Doctrine2 in your Symfony2 or Symfony3 application.
[](https://packagist.org/packages/raksul/doctrine-set-type-bundle)
[](https://travis-ci.org/raksul/DoctrineSetTypeBundle)
[](https://scrutinizer-ci.com/g/raksul/DoctrineSetTypeBundle/?branch=master)
[](https://scrutinizer-ci.com/g/raksul/DoctrineSetTypeBundle/?branch=master)[](https://packagist.org/packages/raksul/doctrine-set-type-bundle)
## Features
* SET type mapping for mysql
* SET type validation
* Doctrine migrations## Requirements
* PHP ~7.2
* Symfony ~2.8 or ~3.0
* Doctrine ~2.3## Supported platforms
* MySQL
## Installation
### Step 1: Download the Bundle
Using composer
```
$ composer require raksul/doctrine-set-type-bundle "1.0.0"
```## Step 2: Enable the Bundle
Then, enable the bundle by adding the following line in the `app/AppKernel.php`
file of your project:```php
'Group 1',
self::GROUP2 => 'Group 2',
self::GROUP3 => 'Group 3',
];
}
```Or you may define set type definition in entity by overrideing `AbstractSetType::getChoices()` method.
```php
class UserGroupType extends AbstractSetType
{
/**
* {@inheritdoc}
*/
public static function getChoices()
{
return User::getGroupChoices();
}
}class User
{
public static function getGroupChoices()
{
return [
self::GROUP1 => 'Group 1',
self::GROUP2 => 'Group 2',
self::GROUP3 => 'Group 3',
];
}
}
```### Register your type
Register UserGroupType in `config.yml`
```yml
doctrine:
dbal:
## ...
types:
UserGroupType: AppBundle\DBAL\Types\UserGroupType
```### Add mapping data to entity
This is annotaion sample.
```php
groups = $groups;return $this;
}/**
* Get groups
*
* @return array
*/
public function getGroups()
{
return $this->groups;
}
}
```You can set Groups with array to User entity
```php
$user->setGroups([UserGroupType::GROUP1, UserGroupType::GROUP2]);
```And also You can validate your type by adding the following annotation.
```php
/**
* @DoctrineAssert\SetType(class="AppBundle\DBAL\Types\UserGroupType")
*/
private $groups;
```### Building the form
Pass `null` to the Second argument.
[SetTypeGuesser](https://github.com/raksul/DoctrineSetTypeBundle/blob/master/Form/Guess/SetTypeGuesser.php) extends ChoiseType and render the field as checkboxes.
So, you can use choice field type option. (see [choice Field Type](http://symfony.com/doc/current/reference/forms/types/choice.html))
```php
$builder->add('groups', null, [
'required' => true,
'invalid_message' => 'Given values are invalid!!'
]);
```### Doctrine migrations
Following SQL is executed.
```sql
CREATE TABLE user (
id INT AUTO_INCREMENT NOT NULL,
username varchar(50) COLLATE utf8_unicode_ci NOT NULL,
groups set('group1','group2') DEFAULT NULL COMMENT '(DC2Type:UserGroupType)',
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB
```### License
This bundle is under the MIT license. see LICENSE:
[LICENSE](https://github.com/raksul/DoctrineSetTypeBundle/blob/master/LICENSE)