doctrineum/boolean

Enumeration type for Doctrine - booleans only

2.2.0 2019-03-01 10:55 UTC

This package is auto-updated.

Last update: 2024-03-29 03:32:52 UTC


README

Build Status Test Coverage License

About

Adds Enum to Doctrine ORM (can be used as a @Column(type="boolean_enum")).

##Usage

<?php

use Doctrine\ORM\Mapping as ORM;
use Doctrineum\Boolean\BooleanEnum;

/**
 * @ORM\Entity()
 */
class Account
{
    /**
     * @var int
     * @ORM\Id() @ORM\GeneratedValue(strategy="AUTO") @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var BooleanEnum
     * @ORM\Column(type="boolean_enum")
     */
    private $activated;
    
    public function __construct()
    {
        $this->activated = BooleanEnum::getEnum(false);
    }

    /**
     * @return BooleanEnum
     */
    public function getActivated()
    {
        return $this->activated;
    }

    /**
     * @param BooleanEnum $activated
     */
    public function setActivated(BooleanEnum $activated)
    {
        $this->activated = $activated;
    }
}

$account = new Account();
$account->setActivated(BooleanEnum::getEnum(true));

/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager->persist($account);
$entityManager->flush();
$entityManager->clear();

/** @var Account[] $accounts */
$accounts = $entityManager->createQuery(
    "SELECT a FROM Account a WHERE a.activated"
)->getResult();

var_dump($accounts[0]->getActivated()->getValue()); // true;

##Installation

Add it to your list of Composer dependencies (or by manual edit your composer.json, the require section)

composer require jaroslavtyc/doctrineum-boolean

Note: NULL is prohibited. NULL means "I don't know" and that is not FALSE neither TRUE. If you want to use NULL, as lets say FALSE, do that conversion by your own, because you are the only one "who knows".

Doctrine integration

Register new DBAL type:

<?php

use Doctrineum\Boolean\BooleanEnumType;

BooleanEnumType::registerSelf();

When using Symfony with Doctrine you can do the same as above by configuration:

# app/config/config.yml

# Doctrine Configuration
doctrine:
    dbal:
        # ...
        mapping_types:
            boolean_enum: boolean_enum
        types:
            boolean_enum: Doctrineum\Boolean\BooleanEnumType