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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭