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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.