PHP code example of kstefan / doctrine-enum

1. Go to this page and download the library: Download kstefan/doctrine-enum 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/ */

    

kstefan / doctrine-enum example snippets


$eventManager->addEventSubscriber(new Enum\Doctrine\EnumSubscriber());



namespace App\Enum;

use Enum\AbstractEnum;

class UserState extends AbstractEnum
{
    const STATE_NEW = 'new';
    const STATE_ACTIVE = 'active';
}



namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Enum\UserState;
use Enum\Doctrine\Mapping\Annotation\Enum;

/**
 * @ORM\Entity
 **/
class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     **/
    protected $id;

    /**
     * @Enum(class="App\Enum\UserState")
     * @ORM\Column(type="string", length=50)
     **/
    protected $state;

    public function __construct()
    {
        // you can set default
        $this->state = new UserState(UserState::STATE_NEW);
    }

    /**
     * @param UserState $state
     */
    public function setState(UserState $state)
    {
        $this->state = $state;
    }

    /**
     * @return UserState
     */
    public function getState()
    {
        return $this->state;
    }
}



use App\Entity\User;
use App\Enum\UserState;

$user = $entityManager->getRepository('App\Entity\User')->find(1);
echo $user->getState()->getValue();
// new
$user->setState(new UserState(UserState::STATE_ACTIVE));
echo $user->getState()->getValue();
// active
$entityManager->flush($user);