Skip to content

Instantly share code, notes, and snippets.

@elenakondrateva
Last active June 28, 2023 00:21
Show Gist options
  • Save elenakondrateva/c3b3bb378ad777ed6ab91ff441611d1d to your computer and use it in GitHub Desktop.
Save elenakondrateva/c3b3bb378ad777ed6ab91ff441611d1d to your computer and use it in GitHub Desktop.
Custom Doctrine DBAL type 'tinyint' for Symfony
<?php
namespace AppBundle\Doctrine\DBAL\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class Tinyint extends Type
{
const TINYINT = 'tinyint';
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$fieldDeclaration = array_merge([
'length' => 1,
], $fieldDeclaration);
return sprintf("TINYINT(%d)",
$fieldDeclaration['length']
);
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return (int) $value;
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return (int) $value;
}
public function getName()
{
return self::TINYINT;
}
public function getBindingType()
{
return \PDO::PARAM_INT;
}
}
@elenakondrateva
Copy link
Author

config.yml for Symfony (2.3):

    doctrine:
        dbal:
            types:
                tinyint:
                    class: AppBundle\Doctrine\DBAL\Types\Tinyint
                    commented: true

@elenakondrateva
Copy link
Author

Entity declaration:

    /**
     * @var integer
     *
     * @ORM\Column(name="status", type="tinyint", options={"default" = 0}, length=1)
     * @Assert\NotBlank()
     */
    private $status;

@devnix
Copy link

devnix commented Oct 30, 2020

@elenakondrateva It generates always the same migration in Symfony 5. Did you get also this issue?

@cezarpopa
Copy link

If someone else runs into issue, make sure that you add your custom type to the mapping_types see following thread on stackoverflow: https://stackoverflow.com/questions/33260367/doctrine-doctrineschemaupdate-command-produces-the-same-set-of-queries-again-a

@muratcakmaksoftware
Copy link

Which one is right for you?

public function getBindingType()
{
    return ParameterType::INTEGER;
}

OR

public function getBindingType()
{
    return \PDO::PARAM_INT;
}

@elenakondrateva
Copy link
Author

@muratcakmaksoftware probably using Doctrine\DBAL\ParameterType would be better option here.
I'm sorry I haven't been working with Symfony or Doctrine for ages now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment