PHP code example of lesha888 / doctrine-set-type-bundle

1. Go to this page and download the library: Download lesha888/doctrine-set-type-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lesha888 / doctrine-set-type-bundle example snippets



// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Okapon\DoctrineSetTypeBundle\OkaponDoctrineSetTypeBundle(),
        );

        // ...
    }

    // ...
}



namespace AppBundle\DBAL\Types;

use Okapon\DoctrineSetTypeBundle\DBAL\Types\AbstractSetType;

class UserGroupType extends AbstractSetType
{
    const GROUP1 = 'group1';
    const GROUP2 = 'group2';
    const GROUP3 = 'group3';

    /**
     * {@inheritdoc}
     */
     protected $name = 'UserGroupType'; // This is Optional. Automatically registered shord class name.

    /**
     * define your SET type.
     */
    protected static $choices = [
        self::GROUP1 => 'Group 1',
        self::GROUP2 => 'Group 2',
        self::GROUP3 => 'Group 3',
    ];
}



namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Okapon\DoctrineSetTypeBundle\Validator\Constraints as DoctrineAssert;
use AppBundle\DBAL\Types\UserGroupType;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=50)
     */
    private $username;

    /**
     * @var array
     *
     * @DoctrineAssert\SetType(class="AppBundle\DBAL\Types\UserGroupType")
     * @ORM\Column(name="groups", type="UserGroupType", nullable=true) // mapping_type
     */
    private $groups;

    // ...

    /**
     * Set groups
     *
     * @param array $groups
     * @return User
     */
    public function setGroups(array $groups)
    {
        $this->groups = $groups;

        return $this;
    }

    /**
     * Get groups
     *
     * @return array
     */
    public function getGroups()
    {
        return $this->groups;
    }
}

$user->setGroups([UserGroupType::GROUP1, UserGroupType::GROUP2]);

$builder->add('groups', null, [
    'choices' => UserGroupType::getChoices()
    '