如何在 cakephp 中编写以下查询 2.x

how to write following query in cakephp 2.x

SELECT categories.category_name, categories.status, experts.name, experts.email, expert_categories.category_id, expert_categories.expert_id 
FROM categories, experts, expert_categories 
WHERE expert_categories.category_id = categories.id AND expert_categories.expert_id = experts.id AND categories.status = 'A'

如果您知道自己想要什么,请使用准备好的语句:

 $db = $this->Category->getDataSource(); // if you make function in categories controller
 // $db = $this->getDataSource(); // if you make function in any model

 $result = $db->fetchAll("SELECT categories.category_name,categories.status,
       experts.name, experts.email, expert_categories.category_id, 
       expert_categories.expert_id FROM categories, experts, expert_categories 
       WHERE expert_categories.category_id = categories.id AND
       expert_categories.expert_id = experts.id AND categories.status = 'A'");

 // debug($result);

这就是cakephp准备语句的原因。

看这里:Cakephp2 prepared statement.

$this->Category->ExpertCategory->bindModel(array('belongsTo' => array(
    'Category' => array(
        'foreignKey' => false,
        'type'=>'INNER',
        'conditions' => array(
            'Category.id = ExpertCategory.category_id
             ExpertCategory.category_id = ' . $cat_id . ' and 
             Category.status = "A"'
         )
    ),
    'Expert' => array(
        'foreignKey' => false,
        'type'=>'INNER',
        'conditions' => array(
            'Expert.id = ExpertCategory.expert_id'
         )
     )
)), false);        

$allExpertArr = $this->Category->ExpertCategory->find('all');