doubi5127 2012-09-20 22:14
浏览 37
已采纳

CakePHP - 根据孩子查找父母

I have three tables companies, products and prices.

Companies hasMany Products
Products hasMany Prices

I'm currently trying to search these tables for products that are less than a certain price, the code I'm trying is:

$results = $this->Product->find('all', array(
    'conditions' => array(
        'Price.price <=' 10
    )
));

For some reason using this code cakephp this brings back an error:

Unknown column 'Price.price' in 'where clause'

I think this is because products have multiple prices (it's joining with the companies table not the prices table), can anyone tell me why this is going wrong and know how to get it working?

Note: After getting this working, I want to be able to find products based on criteria from the prices and products tables, then display data from all 3 in a results page.

I thought about first searching the prices table and then searching the products table, but I believe there must be a more efficient way?

  • 写回答

2条回答 默认 最新

  • duanji9378 2012-09-20 22:19
    关注

    You may use adhoc-joins for these types of queries. Cake doesn't do joins for 1:n or n:n relationships, so you have to specify it manually.

    $results = $this->Product->find('all', array(
        'joins' => array(
            array(
                'alias' => 'Price',
                'table' => 'prices',
                'foreignKey' => false,
                'conditions' => array('Price.product_id = Product.id'),
            ),
        ),
        'conditions' => array(
            'Price.price <=' => 10,
        ),
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题