Tuesday, May 14, 2013

Re: Model capable of Table switching

Create a model "Temp" for example... and insert the lines in your model:

public function beforeFind($queryData) {
        parent::beforeFind($queryData);
        if(isset($queryData['conditions']['key'])){
            $key=$queryData['conditions']['key'];
            unset($queryData['conditions']['key']);
            switch($key){
                case 1:{
                    $this->useTable='dropdowns';
                    $queryData['conditions']['module_id']='5000';
                    $queryData['fields']=array('id','label');
                    break;
                }
                case 2:{
                    $this->useTable='dropdowns';
                    $queryData['conditions']['module_id']='2000';
                    $queryData['fields']=array('id','label');
                    break;
                }
                case 3:{
                    $this->useTable="imp_dmap";
                    $queryData['conditions']['module_id']='2000';
                    break;;
                }
                case 4:{
                    $this->useTable="imp_dmap";
                    $queryData['conditions']['module_id']='2000';
                    break;
                }
                default: break;
            }
        }
        return $queryData;
    }

When you use the find method you will use like this (example):
$data=$this->Model->find('all',array('conditions'=>array('key'=>2,'field'=>'Here your field')));

Sure it´s not the correct way to work with CakePHP, the correct would be using the switch on your controller, and both tables have it´s own Model, but sometimes i think it´s a good run out of the conventions

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: