Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:38:07+00:00 2026-06-18T08:38:07+00:00

I have an App in CakePHP 2.2 & MySQL. I noticed that the type

  • 0

I have an App in CakePHP 2.2 & MySQL.
I noticed that the type casting of the value returned is string for many types like Float, Decimal, Int, Varchar, Text etc..:

Float or Decimal:

var_dump($this->field('field_name'));
string(4) "1.00" 

Int

var_dump($this->field('field_name'));
string(1) "1" 

This problem Not occur only using TINYINT(1) for boolean fields:

Tinyint(1)

var_dump($this->field('field_name'));
bool(true) 

Normally for currency fields I set the field type as FLOAT or DECIMAL(11,2)… maybe Im wrong using this types in CakePHP?
This behavior is very tedious especially with decimal fields because when the value is 0 is returned as string “0.00” that is true.
So I have to always remember to force the type like this:

if((float)$this->field('price')){
....
}

Why CakePHP does not return values ​​with the proper typecasting? How do I fix this? Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:38:09+00:00Added an answer on June 18, 2026 at 8:38 am

    This is NOT a CakePHP issue. This is due to the loose-typing currently used in the PHP scripting langiage. TinyInt and Boolean work since it is converted to the bool type.

    The work around used by all has been to strictly cast the variable you wish to be float to float, as in your example. To check if a string is numeric you can use the is_numeric() function:

    if (is_numeric($testedFloatString)) {
        //If $testedFloatString is a string representing a number cast it to float
        $testedFloatString = (float) $testedFloatString;
    }
    

    This is the concept of “Type Juggling”. What you need to remember for floating point numbers here is that:

    $varName = (float) $varName;
    $varName = (double) $varName;
    $varName = (real) $varName;
    

    will all be casted to PHP’s Float. This issue will cease to exist with the long-awaited PHP 6 strict typing but we’ll have to wait a bit more for that. As for now – the only way is to handle these variables yourself.
    To automatically manage this in CakePHP for the Models that you need use the Model::afterFind() callback. This callback is used “to modify results that have been returned from a find operation or to perform any other post-find logic. Also watch out for the second parameter, as when a Model’s find() is called from an associated Model the resulting array’s structure will be different.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CakePHP app that is being moved to Sql Server from MySql.
I have CakePHP app in which I'd like to attach gallery to multiple resources.
I have a simple cakephp app with table articles that has a cat_id column
In my CakePHP 1.2.5 app, I have a Profile model that belongsTo a User
I have Cakephp app installed on a linux box. The Mysql Driver is installed
I have a rather simple CakePHP app that uses the Auth component to restrict
I have a small cakePHP app that allows for user's to login and be
In my CakePHP app I have static pages set up like this: Router::connect( '/terms',
I have created a cakephp app. I have urls looks like www.mysite.com/products/search/hardware It loads
I have some component code that I'd like to in a CakePHP Task, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.