PHP code example of raksul / doctrine-set-type-bundle
1. Go to this page and download the library: Download raksul/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/ */
raksul / doctrine-set-type-bundle example snippets
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Raksul\DoctrineSetTypeBundle\RaksulDoctrineSetTypeBundle(),
);
// ...
}
// ...
}
namespace AppBundle\DBAL\Types;
use Raksul\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',
];
}
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',
];
}
}
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Raksul\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;
}
}