An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

        

DoctrineSetTypeBundle
=====================

The `DoctrineSetTypeBundle` provides support MySQL SET type for Doctrine2 in your Symfony2 or Symfony3 application.

[![Latest Stable Version](https://poser.pugx.org/raksul/doctrine-set-type-bundle/v/stable.svg)](https://packagist.org/packages/raksul/doctrine-set-type-bundle)
[![Build Status](https://travis-ci.org/raksul/DoctrineSetTypeBundle.svg?branch=master)](https://travis-ci.org/raksul/DoctrineSetTypeBundle)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/raksul/DoctrineSetTypeBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/raksul/DoctrineSetTypeBundle/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/raksul/DoctrineSetTypeBundle/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/raksul/DoctrineSetTypeBundle/?branch=master)

[![License](https://poser.pugx.org/raksul/doctrine-set-type-bundle/license.svg)](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)