cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Bug when Phalcon\Mvc\Model working table has `tinyint(1)` field type

Open temafey opened this issue 12 years ago • 7 comments

When I try to save/update/create row, method returns true, method getMessages returns empty array, but row is not inserted/updated! I run many different tests and found that if my mysql table has field with type 'tinyint(1)' I can't save/update/create row if this field value is integer, but if I change value integer to string everything is inserted/updated normally! (test configuration: Ubuntu 13.04, php 5.5.6, Mysql 5.5.35, phalcon 1.2.5,1.2.6)

Field with type 'tinyint(1)' Phalcon\Mvc\Model\MetaData return '8' type number (TYPE_BOOLEAN constant in Phalcon\Db\Column), but if I change field type to int(1), smallint(1) or tinyint(2) Metadata return '0' type number (TYPE_INTEGER constant in Phalcon\Db\Column)!

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

temafey avatar Feb 10 '14 08:02 temafey

tinyint(1) suppose to be a boolean, and can not save because it is integer. It seems that that best solution will be add some convertial logic when value is bool or int(1 or 0) - save as boolean type in other cases save as int.

lantian avatar Feb 15 '14 08:02 lantian

tinyint(1) suppose to be a boolean

Actually this is a bug — TINYINT(1) can store any value in [-128..127] or [0..255] range.

ghost avatar Feb 15 '14 08:02 ghost

I meant that tinyint(1) is used as boolean, so here must be logic for both - integer and boolean.

lantian avatar Feb 15 '14 09:02 lantian

Just want to chime in confirmation of this bug, assigning a numeric value to column with type TINYINT(1) fails, however no error message and no warning of failure. Casting the numeric value to a string is a solid work-around, however the convention of treating TINYINT(1) as a bool must be secondary to it's primary nature as a 1-byte integer type.

mahelious avatar May 02 '14 19:05 mahelious

TINYINT(1) can store any value in [-128..127] or [0..255] range.

As far as I know:

Values [-128..127] or [0..255] can be stored in TINYINT - there is no (1) at the end(!).

INT can store values from -2147483648 to 2147483647 (or from 0 to 4294967295 for UNSIGNED). It does NOT mean that you can store a value of 4294967295 in a INT(2) UNSIGNED field because is too short.

The same way, you can NOT store 255 in a TINYINT(1) field because is to short. You CAN store 255 in a TINYINT field - to achive this you do NOT add this (1) at the end of the word.

So i think it is not a bug.

Conradaek avatar Jun 10 '14 06:06 Conradaek

Conradaek I think you may be confusing display length and internal range. Please see http://dev.mysql.com/doc/refman/5.0/en/numeric-type-attributes.html

mahelious avatar Jun 10 '14 11:06 mahelious

Fixed in 2.0.x

andresgutierrez avatar Aug 28 '15 21:08 andresgutierrez