tags:

views:

91

answers:

2

Little history; I hate the fact that I can't use enums in CakePHP, but I get it. However, another pet peev I have is that my Booleans return 0 or 1 and there is no way to universally turn them to yes' and no's.

So I though I would create a little function in the afterFind method of the AppModel to do this for me. The first step I wanted to take was to identify which columns where boolean (since some columns will return zeros and ones that do not need to be converted). I devised this little peace of code:

function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);
    foreach($this->_schema as $col => $colDetails){
        if($colDetails['type'] == 'boolean')
            $this->_booleans[] = $col;
    }
}

However a quick debug($this) in the model show that only the current model's boolean columns are captured. When I hit those columns directly the $this->_booleans show up but again, not those of associated models.

I've looked though the manual and the API..I see no mention as to how to approach a solution.

What am I doing wrong?

+2  A: 

Enums are not supported by CakePHP in order to make an application database type independent. Enums are not supported by many database engines. The simplest solution for your task is:

echo $model['boolField'] ? 'Yes' : 'No';
bancer
+1 Do **not** turn booleans into 'Yes' and 'No' in an `afterFind`, just don't. It's code concerning the View, so that's where you need to do this. Otherwise your *business logic* will have to compare values to 'Yes' and 'No' instead of booleans, which is a disaster waiting to happen.
deceze
@bancer and this would go in my view?
Angel S. Moreno
@Angel: That's right. In your view :) Just replace `$model['boolField']` with your boolean field.
bancer
@deceze so you are saying that is it sthe View's responsibility to interpret booleans as 'Yes' and 'No' rather than the model? I would think the model should handle the interpretation of data.
Angel S. Moreno
@Angel No, the Model is responsible for *handling* data, not for displaying it. The View is responsible for turning the raw data into human readable output.
deceze
well, when you put it that way.. I got my answer. thanks guys
Angel S. Moreno
A: 

The problem is that $this->_booleans in the AppModel only contains the schema details of the current model. In fact, the code is probably working. You should check $this->_booleans and $this->Related->_booleans, and I bet you'll find what you're looking for.

Stephen
I wish that were the case Stephen. However, like I mentioned, I did a debug($this), saw the $this->Related->_booleans array but they where empty arrays :-(, when I accessed that model $this->_booleans was populated but all the $this->Related->_booleans were once again empty arrays.
Angel S. Moreno
Hmm... Let me think about your problem again.
Stephen