Symfony / Doctrine: Why is my integer Entity attribute returned as a string?

I have an Entity with an attribute defined as follows:

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

I tried to solve the error and used var_dump () to find out what was going on ...

Answer:

 string '20' (length=2) 

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

 int 20 

Or should I handle this in my business logic?


Edit (as I called var_dump ()):

I have a class called "Calculator" that iterates over $ items and uses the $ weight attribute. Something like that:

Controller:

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

Calculator:

 foreach($items as $item) { //logic... var_dump($item->getWeight()); } 

Edit (database column layout):

Here is the field in the database:

 weight int(11) 
+5
source share
1 answer

What version of PHP and MySQL driver are you using?

I don’t know exactly how Symfony produces (or not) the database results, but if you use PHP 5.2, this can be quite normal: any variable that you get from MySQL to PHP is always a string, even if it is an integer in database.

In PHP 5.3, it is 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 information: fooobar.com/questions/282358 / ...

Hope this helps!

0
source

Source: https://habr.com/ru/post/1237063/


All Articles