{"id":2539,"date":"2017-06-26T16:33:53","date_gmt":"2017-06-26T16:33:53","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=2539"},"modified":"2024-11-28T14:12:15","modified_gmt":"2024-11-28T14:12:15","slug":"add-tinyint-mysql-doctrine-symfony-2-8","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/add-tinyint-mysql-doctrine-symfony-2-8\/","title":{"rendered":"How to add TINYINT MySQL type to Doctrine in Symfony 2.8"},"content":{"rendered":"<p>Hello! A few days ago I needed to define an entity with a TINYINT type column to hold a rating value between 1 and 5. I was doing this in a <a href=\"http:\/\/symfony.com\/blog\/symfony-2-8-0-released\">Symfony 2.8 <\/a>project and to my surprise, Doctrine was unable to handle this type of data out of the box (it uses it for booleans but it has no TINYINT standalone type).<\/p>\n<p>So I searched the internet for a solution and I found some but nothing complete.<\/p>\n<h2>The solution (TINYINT vs Doctrine):<\/h2>\n<p>First you need to define the new Doctrine type class like this:<\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\r\nnamespace AppBundle\\Types;\r\n\r\n\r\nuse Doctrine\\DBAL\\Types\\Type;\r\nuse Doctrine\\DBAL\\Platforms\\AbstractPlatform;\r\n\r\n\r\nclass TinyintType extends Type\r\n{\r\n    const TINYINT = 'tinyint';\r\n\r\n\r\n    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)\r\n    {\r\n        return 'TINYINT';\r\n    }\r\n\r\n\r\n    public function convertToPHPValue($value, AbstractPlatform $platform)\r\n    {\r\n        return $value;\r\n    }\r\n\r\n\r\n    public function convertToDatabaseValue($value, AbstractPlatform $platform)\r\n    {\r\n        return $value;\r\n    }\r\n\r\n\r\n    public function getName()\r\n    {\r\n        return self::TINYINT;\r\n    }\r\n}<\/pre>\n<p>Then, to register it, I used the boot function of the AppBundle:<\/p>\n<pre class=\"lang:php decode:true\">&lt;?php\r\nnamespace AppBundle;\r\n\r\n\r\nuse Symfony\\Component\\HttpKernel\\Bundle\\Bundle;\r\nuse Doctrine\\DBAL\\Types\\Type;\r\n\r\n\r\nclass AppBundle extends Bundle\r\n{\r\n    public function boot()\r\n    {\r\n        parent::boot();\r\n        \r\n        $em = $this-&gt;container-&gt;get('doctrine.orm.default_entity_manager');\r\n \r\n        \/\/ types registration\r\n        if(!Type::hasType('tinyint')) {\r\n            try {\r\n                Type::addType('tinyint', 'AppBundle\\Types\\TinyintType');\r\n                $em-&gt;getConnection()-&gt;getDatabasePlatform()-&gt;registerDoctrineTypeMapping('TINYINT', 'tinyint');\r\n            } catch (\\Exception $e) {\r\n                \r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>I had to use the try \/ catch block to avoid an error when running symfony console commands having no database defined:<br \/>\n<code><br \/>\nPHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] [1049] Unknown database 'rating'' in vendor\/doctrine\/dbal\/lib\/Doctrine\/DBAL\/Driver\/PDOConnection.php:43<\/code><\/p>\n<p>Now you can use the new type in your entities:<\/p>\n<pre class=\"lang:php decode:true \">    \/**\r\n     * @ORM\\Column(name=\"rating\", type=\"tinyint\")\r\n     * @Assert\\NotBlank()\r\n     * @Assert\\GreaterThanOrEqual(\r\n     *     value = 1\r\n     * )\r\n     * @Assert\\LessThanOrEqual(\r\n     *     value = 5\r\n     * )\r\n     *\/\r\n    protected $rating;<\/pre>\n<h6><a href=\"https:\/\/intelligentbee.com\/blog\/setup-docker-machine-symfony-development\/\">Learn how to setup Docker machine for Symfony development.<\/a><\/h6>\n<p>Hope this helps someone \ud83d\ude42<br \/>\nHave a great day!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! A few days ago I needed to define an entity with a TINYINT type column to hold a rating [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":2540,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75,82],"tags":[117,123,175,194,238],"yst_prominent_words":[564,584],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2539"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=2539"}],"version-history":[{"count":1,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2539\/revisions"}],"predecessor-version":[{"id":133341,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2539\/revisions\/133341"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media\/2540"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=2539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=2539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=2539"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=2539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}