簡體   English   中英

Cakephp3,如何在cakephp3中使用where子句和內連接?

[英]Cakephp3, How to use where clause and inner join in cakephp3?

我正試圖從產品表中獲取評論表。 我添加了列名'delete_yn',這意味着刪除了評論。 我在下面使用了一些蛋糕查詢

return $this->Product->find()
        ->contain(['ProductReview', 'Users'])
        ->where(['Product.product_code' => $productCode])
        ->toArray();

結果很好。

但是,現在我想檢查用戶是否刪除了評論,並在“del_yn”列中僅顯示“n”我添加了此查詢

->andWhere(['Product.ProductReview.del_yn' => 'n'])

在where子句之后。

但它不起作用。

請幫忙。

而不是使用andWhere(['Product.ProductReview.del_yn' => 'n']) ,使用matching (如果cakephp 3.0.x)或innerJoinWith() if(cakephp 3.1.x)並過濾匹配特定關聯數據的記錄。 例如,

return $this->Product->find()
            ->matching('ProductReview', function ($q) {
               return $q->where(['ProductReview.del_yn' => 'n']);
            })
            ->contain(['ProductReview', 'Users'])
            ->where(['Product.product_code' => $productCode])
            ->group('Product.id')
            ->toArray();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM