[DDC-2807] Add support for char fields in the ORM layer Created: 24/Jan/10  Updated: 21/Nov/13  Resolved: 21/Nov/13

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: None
Affects Version/s: 2.1, 2.1.1, 2.1.2
Fix Version/s: 2.4
Security Level: All

Type: Improvement Priority: Minor
Reporter: Glen Ainscow Assignee: Roman S. Borschel
Resolution: Fixed Votes: 0
Labels: None


 Description   

It's not possible to use char fields in the ORM layer.

It should be possible to use something like:

@Column(type="char") or ...
@Column(type="string", fixed=true) or ...
@Column(type="fixedstring")



 Comments   
Comment by Roman S. Borschel [ 24/Jan/10 ]

I dont think this is worth including in the main distribution. A char does not save much compared to a varchar (1 Byte?) and you already have 2 options to make a char:

  • create your own custom type (i.e. FixedString)
  • Use @Column(..., columnDefinition="CHAR(2)")

IMHO, just use a string type with the length you want: @Column(type="string", length=2). That becomes a varchar with length 2.

Comment by Glen Ainscow [ 24/Jan/10 ]

Ya, it is only 1 extra byte. I'm not sure what to do, I like things as optimized as possible, but I guess I could just use columnDefinition if necessary.

Comment by Glen Ainscow [ 24/Jan/10 ]

Actually, I don't really see any reason not to include the char type.

Comment by Roman S. Borschel [ 24/Jan/10 ]

How about code bloat?

Comment by Glen Ainscow [ 24/Jan/10 ]

How many LoC?

Comment by Guilherme Blanco [ 25/Jan/10 ]

@darkangel Around 40. And lots of conditionals, which decreases efficiency of algorithm.

I vote for FixedString DBAL DataType.

Comment by Roman S. Borschel [ 25/Jan/10 ]

We will not put every special data type someone comes up with in the core library. If we go this route, at the end we have 100+ data types (100+ classes plus a bloated type map) in the core library.

There are at least 2 decent options of making a char already if you care about byte counting (see above).

"Why not?" is not the question to ask for when it comes to new features. If it were, we would include a whole lot of stuff that is useless for 99% of the users. There must be strong arguments for "Why?" and there are none.

If we get 50+ votes on this issue we can talk again.

Comment by Glen Ainscow [ 25/Jan/10 ]

Of course not. I didn't know that char was a special data type (especially since it's supported in DC1.2).

I will use @columnDefinition.

You may close this issue.

Comment by Roman S. Borschel [ 25/Jan/10 ]

No need to become defensive. There is still the chance that demand for this particular type gets very high and that can change things.

Thats why this stays open. Otherwise the next guy would probably just create a duplicate ticket (not sure whether non-owners can reopen other tickets).

Comment by Benjamin Eberlei [ 25/Jan/10 ]

@Glen

The problem with Doctrine 1 and having lots of different data-types is that of maintainability. You have to ensure that all the types work on all supported platforms with each and every version. The more datatypes we support by default the more complex will it be for the Doctrine 2 Core to ensure compability and maintainability in this regard.

For each new platform that we will support all datatypes have to be supported for example, something that might even become impossible for some databases.

Adding a datatype from the user perspective is rather simple though, it has to be tested once and is only about 20-40 LOC. I bet you 100 bucks that soon there will be code-snippets out there on all the different database specific types as a doctrine 2 implemention.

Comment by Glen Ainscow [ 25/Jan/10 ]

@Roman
Not being defensive, I have accepted/respected your decision.

@Benamin
I did think about cross-database compatibility. As far as I know, char is supported by the vast majority (or at least easily simulated). But I understand.

Thanks.

Comment by Dmitry Strygin [ 17/Sep/11 ]

Sorry for party rocking but i think that 'fixed' annotation should be enabled in ORM column definitions.
In this case @Column(type="string", length=4, fixed=true) would result in "CHAR(4)" for MySQL, Oracle and Postgres, Sqlite, and DB2, "NCHAR(4)" for MQSQL. etc... It is much more elegant than that columnDefinition.
It is already supported by DBAL as well as 'default' heartlessly removed (DDC-100) from annotation driver due to bizzare reluctance to deal with proper escaping of values.

Comment by Steve Müller [ 24/Jun/13 ]

As Dmitry stated, this is fixed by the "fixed" property. Shouldn't this be closed?

Comment by Steve Müller [ 21/Nov/13 ]

Fixed in 2.4.0:

https://github.com/doctrine/doctrine2/commit/cfee331006c0011ca09250bb7ba4e07c32bb68ad

You can pass column options:

@Column(type="string", options={"fixed" = true})

This generates a CHAR instead of VARCHAR column.

Generated at Wed Sep 03 05:16:59 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.