0

Trying to make cakephp to return tinyint(1) as int and not as bool. I don't want to change my entire database for small select boxes, just to fit it to CakePHP.

I found the following row at lib/Cake/Model/Datasource/Database/Mysql.php

if (($col === 'tinyint' && $limit == 1) || $col === 'boolean') {
        return 'boolean';
    }

Commenting this line didn't work.

Anyone have a solution for this issue?

5
  • In MySQL a bool/bit field is really a int(1) field. Commented Sep 19, 2012 at 13:27
  • Just cast it as an int Commented Sep 19, 2012 at 13:28
  • In what debug level are you ? If it is set to 0, the field type may not be updated as it is taken from the cache Commented Sep 19, 2012 at 13:44
  • nlcO i'm still building it, and not debugging. just fetch existing db Commented Sep 19, 2012 at 14:20
  • Don't! tinyint(1) is always a boolean in Cake. If you need integers (as for enums or other small entities of numbers), use tinyint(2) or tinyint(3) instead! Also, when working with checkboxes, boolean values are just fine. You are creating problems where there are none. Commented Aug 8, 2013 at 9:07

6 Answers 6

2

Don't touch the framework. Override the afterFind() method of the model in question and cast your fields to the proper type.

Sign up to request clarification or add additional context in comments.

3 Comments

tried to check the result that afterFind receive. Cake changes the value of the field before that function runs. so table.fld = 4 in the db, is shown as 1.
Well, eventually i found inside the function resultSet the following line $type = ($column['len'] == 1) ? 'boolean' : 'string'; which did the "magic". thanks all for your help.
@YK nice, it is here in this file /lib/Cake/Model/Datasource/Database/Mysql.php
1

Do you need this to stop Cake making your inputs into check boxes?

Just do array('type'=>'text') on the input()

5 Comments

i dont have inputs here, i have select box that have 10 values and they're permanent. trying to display the fields value, but getting 1\0.
post your actual code; this is unlikely to do with data formats.
inside the model file function getUserMusic($id) { return $this->find('first', array( 'fields' => array( ), 'conditions' => array( 'Music.userid' => $id ), )); } inside the controller file $this->set('music', $this->Music->getUserMusic($userid)); inside the view file debug($music);
That all looks fine, just do pr($music); in the view and take whatever info you need from the array?
the problem is this, in the db the field looks like that array('genre1' => 2, 'genre2' => 3, 'genre3' => 5); after using find the fields looks like that array('genre1' => 1, 'genre2' => 1, 'genre3' => 1)
0

Never modify the core unless you plan to contribute the changes. If you absolutely have to override the MySQL Datasource, you can do so without modifying the core by copying the file

from

/lib/Cake/Model/Datasource/Database/Mysql.php

into

/app/Model/Datasource/Database/Mysql.php 

and making your changes to the copy. Cake will automatically use this copy you provide rather than what is in the core.

Only do this as a last resort. You'll become responsible for maintaining your own copy of the MySQL driver, and you won't receive security and performance updates to it unless you patch it yourself.

Comments

0

You don't need to change anything in Cake. Just update your MySQL table, and change the column type from 'tinyint' to 'int'. It will store in MySQL the same way, and Cake will automatically know to return the int value, rather than the boolean (true/false).

Remember to delete your model cache files, after updating the MySQL database, in case the app is still reading from the cache.

1 Comment

But then he loses the automagic regarding checkboxes in forms. I don't see any point in messing with the type here.
0

If you change the column length to a value greater than 1, then CakePHP will return the column as a string rather than a boolean. TINYINT can be up to 3 characters long, so you can simply run the following query on your table while keeping the TINYINT format:

ALTER TABLE `your_table_name` MODIFY `your_column_name` TINYINT(3);

1 Comment

See my comment above as well.
0

If for some reason you don't want to change the type of the column then you can cast and get the desired value, below code can serve as alternative.

FYI: registered = tinyint(1)

$this->MODEL_NAME->query("SELECT id,CAST(registered AS SIGNED) as registered FROM users where id = 11223344;");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.