Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a single char column in Doctrine 2

How can I map a single char column in Doctrine 2, using annotations? I would like to have a char type, instead a single char string.

like image 371
Guillermo Gutiérrez Avatar asked Jan 09 '14 15:01

Guillermo Gutiérrez


3 Answers

You can always use the string type with the fixed option:

/**
 * @Column(type="string", length=2, options={"fixed" = true})
 */
protected $country;

The above code snippet produces the following SQL:

`country` char(2) NOT NULL,
like image 143
Francesco Casula Avatar answered Nov 11 '22 11:11

Francesco Casula


Doctrine doesn't have a CHAR type defined out of the box, however it does allow you to define custom types, which you could use to create a 'char' type to use in annotations.

The Doctrine documentation has an example of this: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#custom-mapping-types

like image 2
Kevin Sharp Avatar answered Nov 11 '22 11:11

Kevin Sharp


You might end up providing your own full-column definition:

/**
 * @Column(type="string", columnDefinition="CHAR(2) NOT NULL")
 */
protected $country = null;
like image 2
Антон Кучеревский Avatar answered Nov 11 '22 13:11

Антон Кучеревский