开发者

How do I get associated models to run __construct?

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 look开发者_运维知识库ed though the manual and the API..I see no mention as to how to approach a solution.

What am I doing wrong?


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';


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜