Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use char instead of varchar when generating db tables from entities?

I'm using string(32) instead of integer for entity id field (it simulates guid type). But after creating database table from entity ids field type is set to varchar(32). I think it's not the best idea, guid always has length = 32 so char type will be more appropriate.

Is there a way to tell Doctrine to use char or the only way is to change it manually in database?

like image 478
zelazowy Avatar asked Jul 26 '13 07:07

zelazowy


2 Answers

Although the althaus solution works fine I think this one should be the canonical way of doing it:

With annotations:

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

With YAML (example of both ID and non-ID field):

Your\Nice\Entity:
    id:
        id:
            type: string
            length: 32
            options:
                fixed: true
    fields:
        country:
            type: string
            length: 2
            options:
                fixed: true
  • options: Array of additional options:
    • fixed: Boolean value to determine if the specified length of a string column should be fixed or varying (applies only for string/binary column and might not be supported by all vendors).

Official documentation

like image 80
Francesco Casula Avatar answered Oct 15 '22 05:10

Francesco Casula


You can tell Doctrine to use vendor specific field types:

/**
 * @Column(type="string", columnDefinition="CHAR(2) NOT NULL")
 */
protected $country;

Source

like image 23
althaus Avatar answered Oct 15 '22 07:10

althaus