fetching an Integer from DB using Zend Framework returns the value as a string

后端 未结 4 1933
温柔的废话
温柔的废话 2021-02-20 07:42

When I run an sql query using the ZF wrappers, all the numeric values return as strings. What do I need to change so the values will return in the same data type as they are in

相关标签:
4条回答
  • 2021-02-20 08:14

    I implemented a lot of the Zend_Db code in Zend Framework.

    As other have stated, the reason that Zend_Db returns strings instead of native PHP integers or floats is that PHP's database extensions return strings. And the reason for that is that there might be no native PHP type to represent certain database type.

    For example, MySQL's BIGINT is a 64-bit signed integer. By default, the PHP int type is limited to 32-bit values, so if you fetch data from the database and implicitly convert it to int, some values might be truncated. There are several other similar cases, for float and dates, etc.

    Using the string representation for all data types is the best way to remain simple and consistent, be safe about avoiding data loss, and avoid writing lots of vendor-specific special-case code to do data type mapping. That extra code would incur a performance penalty, too.

    So if you have specific cases where you need database results to be mapped to native PHP data types, you should implement it yourself in your application code (e.g. in a custom Zend_Db_Table_Row class).

    0 讨论(0)
  • 2021-02-20 08:15

    For an example of using a custom Zend_Db_Table_Row to get correct data types, as Bill Karwin suggested, have a look at the class here: http://www.zfsnippets.com/snippets/view/id/70

    It can be implemented in your model as:

    class YourTableName extends Zend_Db_Table_Abstract
    {
        protected $_name = 'your_table_name';
        protected $_rowClass = 'Model_Row_Abstract';
    }
    

    You may want to change Model_Row_Abstract's datatype for tinyint to bool if you use it strictly to hold boolean values.

    0 讨论(0)
  • 2021-02-20 08:16

    Databases typically return result sets as text. Unless your db adaptor converts things for you (and to sounds like yours does not), all values will come back as strings--dates, enums, etc. as well as integers.

    If you are dealing with a small number of tables with only a few integer fields, just hand convert them. If you are dealing with a slightly more complex situation, you could iterate through the columns using the database definitions (see sqlite_fetch_column_types(), etc.). If your situation is more complex than seems reasonable for these solutions, consider switching to a more featureful framework.

    0 讨论(0)
  • 2021-02-20 08:23

    It appears as if this has been requested in the past but has not yet been implemented. #ZF-300 was last commented on on 9 Jan 09.

    Maybe you could share why you are wanting to perform the typecasting and we could help you out another way? PHP is pretty lenient when it comes to variable datatypes...

    0 讨论(0)
提交回复
热议问题