Last active
June 28, 2023 00:21
-
-
Save elenakondrateva/c3b3bb378ad777ed6ab91ff441611d1d to your computer and use it in GitHub Desktop.
Custom Doctrine DBAL type 'tinyint' for Symfony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Entity declaration:
/**
* @var integer
*
* @ORM\Column(name="status", type="tinyint", options={"default" = 0}, length=1)
* @Assert\NotBlank()
*/
private $status;
@elenakondrateva It generates always the same migration in Symfony 5. Did you get also this issue?
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
Which one is right for you?
public function getBindingType()
{
return ParameterType::INTEGER;
}
OR
public function getBindingType()
{
return \PDO::PARAM_INT;
}
@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
config.yml for Symfony (2.3):