Reputation: 872
I'm fairly new to CakePHP, but I find it very easy to use so far. today I've encoutered a problem with data exporting from the db.
I'm exporting 1 of my models as JSON. Everything goes pretty well, except that I noticed that CakePHP exports the id column as string. The model is defined as int in mysql. I've checked both at the JSON output and in the model, by -
var_dump($this->Post->findById($id));exit;
and in the model too, the id is defined as a string.
This messes with other systems that rely on that JSON output, which are used to get an integer as ID.
I tried setting the $_schema attribute in the model, but this didn't change anything.
I would appreciate your help.
Thanks, Dan
Upvotes: 5
Views: 1618
Reputation: 21743
and the ticket: http://cakephp.lighthouseapp.com/projects/42648/tickets/2485-model-find-results-are-all-returned-as-string-fields-even-with-int11-fields
it is a known issue in cake2.x (and based on the way the database returns those) and might be solved in cake3 with the new datasource. although it doesnt look like it because there would be inconsistencies and it would create overhead to cast manually all over the found records...
until a solution is found you want to cast manually in your afterFind callback. Using schema() you are able to find out each field type. if you use it on multiple models, put it into your AppModel.
you could also write a behavior to do the job for you in a clean way - so all you got to do in each model would be:
public $actsAs = array('CastConvert'); //name it whatever you like
the models you dont want to be affected you just don't attach it. careful with associated records. that might also be a limitation of cake. it usually only triggers callbacks for the primary record. in those cases you might want to manually trigger the behavior callback then. If you name it cast() you can access it directly on your models:
$this->Primary-Secondary->cast()
or sth.
Upvotes: 6