我如何使用where子句在cakephp中正确嵌套查询?

问题描述 投票:0回答:1

我正在尝试从订单表中获取包含产品表中产品网址的订单。我尝试使用此子查询,但cakephp仍将URL值s20_ultra读取为:c0

$temp = $this->Orders->getAssociation('Product')->find()->where(['product.url' => 's20_ultra']);
$result = $this->Orders->find()->where(["productId IN" => $temp]);
echo $result;

firstOrFail()附加到第一个查询temp的末尾并注释掉第二个查询result作为测试是可以的,但是当我将其附加到两个时,我得到无法将类型%s的值转换为整数异常。这将是我要完成的正常查询:

select * from `order` where productId in (select product.id from product where url = 's20_ultra')
php cakephp
1个回答
0
投票

根据您的发言,我认为以下内容应能奏效。这假定在OrdersProduct之间的两个方向上都正确建立了关联。 (请注意,这是一个非标准的命名方案,通常为Products。)

$product = $this->Orders->getAssociation('Product')
    ->findByUrl('s20_ultra')
    ->contain(['Orders'])
    ->firstOrFail();
debug($product->orders);
© www.soinside.com 2019 - 2024. All rights reserved.