【问题标题】:how to write following query in cakephp 2.x如何在 cakephp 2.x 中编写以下查询
【发布时间】:2017-01-24 12:32:17
【问题描述】:
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'

【问题讨论】:

  • 我不知道该怎么做。
  • 我是 cakephp 新手
  • 在这种情况下,您应该花一些时间阅读文档。试试看如何retrieve data in CakePHP 2

标签: cakephp cakephp-2.x


【解决方案1】:

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

 $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

【讨论】:

  • 不客气,如果该答案有效,您可以通过单击答案左侧的正确图标来接受。
  • 我找到了完美的解决方案
【解决方案2】:
$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');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多