【问题标题】:Use of condition in query builder in case of multiple association in cakephp 3.0?在 cakephp 3.0 中多重关联的情况下,在查询生成器中使用条件?
【发布时间】:2017-06-14 13:26:40
【问题描述】:

当我在 cakephp 3.0 中使用多个包含中的条件时,我在使用查询生成器进行多级关联时遇到了麻烦。假设我必须获取单个商店的数据,所以我试图在条件下添加 storeId 但它不起作用,否则其余数据正在从下面的所有商店中正确获取是我正在使用的查询生成器:-

// generate query               
$bestSellingReportData = $this->ArticleMaster->find('all')->contain([
    'Category' => [
        'fields' => ['Cat_Id', 'Cat_Code']
    ],
    'size_category' => [
        'fields' => ['sizeCat_Id', 'sizeCat_Code']
    ],                  
    'ItemMaster' => [
        'Invoicedetaile' => [
            'Invoice' => [
                'Store' => [
                    'fields' => ['Store_Id', 'Store_Code']
                ],
                'conditions' => ['Invoice.StoreId ='.$this->request->data['storeId']],
            ],
        ],
    ],
]);

$bestSellingReportData->select(['totalSoldItems' => $bestSellingReportData->func()->sum('Invoicedetaile.Qty')])
->matching('ItemMaster.Invoicedetaile', function ($q) {
    return $q->where([
        'AND' => [
            'Invoicedetaile.ItemId = ItemMaster.Item_ID',
        ]
    ]);
})
->group(['ArticleMaster.Article_Code'])
->order(['totalSoldItems' => 'DESC'])
->autoFields(true);

我尝试使用 add 条件和 where 子句以两种方式添加条件。但是数据不是根据条件过滤的,即 storeId

【问题讨论】:

  • 不起作用”不是正确的问题描述。即使问题对于了解 CakePHP 内部结构的人来说可能很明显,请始终尽可能具体地说明确切会发生什么,以及您希望确切会发生什么而是发生(即您收到什么数据,您需要什么数据,正在生成什么查询,以及它应该是什么样子)。显示您正在使用的数据(例如架构和关联设置)、您的调试尝试以及可能的错误。
  • 没有任何错误我试图从几个表中生成最畅销的报告,即 ArticleMaster、ItemMaster、Invoice、Invoice Detail,现在它正在获取所有数据,我必须根据storeId (shop) 低于我在查询'conditions' => ['Invoice.StoreId ='.$this->request->data['storeId']] 中传递的条件,则数据应仅基于此条件过滤条件对我不起作用,这就是问题所在。
  • 这仍然不是很清楚,只是作为对未来问题的提示,查看您的答案,您想通过关联的ArticleMaster 过滤特定商店的Invoices。从您的示例中很难判断,因为看起来您可能想要过滤容器。

标签: mysql cakephp-3.0 query-builder cakephp-3.x


【解决方案1】:

在做了几个小时的研发后为我工作。下面是适合我的查询生成器。

   $bestSellingReportData = $this->ArticleMaster->find('all')->contain([
    'Category' => [
        'fields' => ['Cat_Id', 'Cat_Code']
    ],
    'size_category' => [
        'fields' => ['sizeCat_Id', 'sizeCat_Code']
    ],                  
    'ItemMaster' => [
        'Invoicedetaile' => [
            'Invoice',
        ],
    ],
]);

$storeId = $this->request->data['storeId'];
$bestSellingReportData->select(['totalSoldItems' => $bestSellingReportData->func()->sum('Invoicedetaile.Qty')])
->matching('ItemMaster.Invoicedetaile.Invoice', function ($q) use ($storeId) {
    return $q->where([
        'AND' => [
            'Invoicedetaile.ItemId = ItemMaster.Item_ID',
            'Invoice.StoreId ='.$storeId,
        ]
    ]);
})
->group(['ArticleMaster.Article_Code'])
->order(['totalSoldItems' => 'DESC'])
->autoFields(true);

【讨论】:

  • 然而,这是一个 SQL 注入漏洞!切勿将(用户)数据直接插入 sn-ps,始终使用 key => value 语法或绑定! 'Invoice.StoreId' => $storeIdbook.cakephp.org/3.0/en/orm/…
  • @ndm 好的,我知道我应该像 'Invoice.StoreId' => $storeId 一样使用它,你能告诉我我应该如何使用这些条件,即 'Invoicedetaile.ItemId = ItemMaster.Item_ID' , ?
  • 该值中没有外部/用户数据,因此它是安全的。如果需要,可以使用表达式重写它:stackoverflow.com/questions/43725445/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多