我正在尝试从订单表中获取包含产品表中产品网址的订单。我尝试使用此子查询,但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')
根据您的发言,我认为以下内容应能奏效。这假定在Orders
和Product
之间的两个方向上都正确建立了关联。 (请注意,这是一个非标准的命名方案,通常为Products
。)
$product = $this->Orders->getAssociation('Product')
->findByUrl('s20_ultra')
->contain(['Orders'])
->firstOrFail();
debug($product->orders);