5

I have an Entity with an attribute defined as following:

/**
 * @var integer
 *
 * @ORM\Column(name="weight", type="integer")
 */
private $weight;

I was trying to solve a bug and used var_dump() to have some clue of what was happening...

The response was:

string '20' (length=2)

I don't understand why $weight is returning as string... shouldn't it be an integer?

int 20

Or should I handle it in my business logic?


Edit (how I called var_dump()):

I have a class named "Calculator" that iterates $items and use the $weight attribute. Is something like this:

Controller:

$calculator->calculate($category->getItems());

Calculator:

foreach($items as $item) {
    //logic...

    var_dump($item->getWeight());
}

Edit (database column schema):

Here is the field in database:

weight  int(11) 

1 Answers1

0

What version of PHP and MySQL driver are you using ?

I don't know exactly how Symfony casts (or not) the database results, but if you're using PHP 5.2, it can be quite normal : any variable you get from MySQL in PHP is always a string, even if it's an integer in the database.

In PHP 5.3, it's different, if you use the correct driver (mysqlnd — See http://blog.ulf-wendel.de/2008/pdo_mysqlnd-the-new-features-of-pdo_mysql/)

See this answer for more details : https://stackoverflow.com/a/2430657/1741150

Hope that helps !

Community
  • 1
  • 1
tchap
  • 3,412
  • 3
  • 29
  • 46