Int columns are pulled as a row from a database in CakePHP

I'm new to CakePHP, but I find it very easy to use so far. Today I ran into the problem of exporting data from db.

I export 1 of my models as JSON. Everything is going fine, except that I noticed that CakePHP exports the id column as a string. The model is defined as int in mysql. I checked both the JSON output and the model, -

var_dump($this->Post->findById($id));exit; 

and also in the model id is defined as a string.

This is due to other systems that rely on this JSON output, which are used to get an integer as an identifier.

I tried to set the $ _schema attribute in the model, but that didn't change anything.

I would be grateful for your help.

Thanks Dan

+4
source share
1 answer

see http://cakephp.1045679.n5.nabble.com/When-retrieving-data-fields-returned-as-strings-and-the-tale-of-climbing-a-mountain-to-cast-those-str -td4312868.html

and ticket: http://cakephp.lighthouseapp.com/projects/42648/tickets/2485-model-find-results-are-all-returned-as-string-fields-even-with-int11-fields

this is a known issue in cake2.x (and based on how the database returns them), and can be solved in cake3 with a new data source. although this is not like because there would be inconsistencies, and this would create overhead to manually overlay all found entries ...

until a solution is found that you want to use manually in your afterFind callback. Using schema (), you can recognize each type of field. if you use it on multiple models, put it in your AppModel.

you can also write behavior to complete the task for you in a clean way - so all you have to do in each model would be:

 public $actsAs = array('CastConvert'); //name it whatever you like 

Models you donโ€™t want to influence, you just donโ€™t attach them. carefully with the appropriate entries. which may also be a cake restriction. it usually only calls callbacks for the primary record. in such cases, you can manually activate the behavior callback. If you call it cast (), you can access it directly on your models:

 $this->Primary-Secondary->cast() 

or sth.

+5
source

All Articles