在where子句中使用FIND_IN_SET的Yii2 Active Record

时间:2018-02-09 10:53:07

标签: javascript php mysql yii yii2

产品表格结构和数据 -

  

id,product_name,hsn,product_category,product_subcategory,company,   pic,part_no,min_stock,stock,rate,notes

product_subcategory列包含类似于例如

的值
13,14,31,138
18
126,140,176,177
78,79

以上数字是下表的ID

product_subcategory - id,subcategory_name

创建发票时,我需要通过product_category, product_subcategory, company的组合搜索产品。

为此,我有如下的ajax

<?php
$script2 = <<< JS
$(document).ready(function () {
$(document.body).on('change', '#purchaseitems-0-category_id, #purchaseitems-0-model_id, #purchaseitems-0-company_id', function () {

    var tt = $("#purchaseitems-0-category_id").val();
    var tt2 = $("#purchaseitems-0-model_id").val();
    var tt3 = $("#purchaseitems-0-company_id").val();

    var stuff1 ={'key1': tt ,'key2': tt2, 'key3': tt3};     
    p1();
});
});

function p1() {
        var stuff ={'key1':$("#purchaseitems-0-category_id").val(),'key2':$("#purchaseitems-0-model_id").val(), 'key3': $("#purchaseitems-0-company_id").val()};

        $.ajax({
            type: "POST",
            url: "http://localhost/yii-application/backend/web/index.php?r=purchase/p1",
            data: {result:JSON.stringify(stuff)},
            success: function (test4) {

            var json_obj5 = $.parseJSON(test4);

            $('#purchaseitems-0-name_of_product').val(json_obj5.id);
            $('#purchaseitems-0-hsn').val(json_obj5.hsn);
            $('#purchase-taxrate').val(json_obj5.rate);
            $('#purchaseitems-0-part').val(json_obj5.part_no);
                    },
                    error: function (exception) {
                        alert(exception);
                    }                   

                });
    }
 JS;
  $this->registerJs($script2);
 ?>

现在需要更改的真实代码 -

public function actionP1()
    {
        $data2 = Yii::$app->request->post('result');
        $data    = $_POST["result"];
        $data    = json_decode("$data", true);
        if (isset($data)) {
            $test = $data;
            $modelfedbkshiprate = \backend\models\Product::find()->where(['product_category' => $data["key1"]])->andWhere(['like' , 'product_subcategory' , $data["key2"]])->andWhere(['company' => $data["key3"]])->one();
        } else {
            $test = "Ajax failed";
        }
        return \yii\helpers\Json::encode($modelfedbkshiprate);          
    }

上述问题的问题是like中的 product_subcategory 运算符。 like运算符搜索id 7而不是78,因此我最终得到了错误的产品。我想过使用FIND_IN_SET但不知道怎么做。

请为like运营商建议替代解决方案。

2 个答案:

答案 0 :(得分:1)

你需要使用FIND_IN_SET来实现这一点,因为我知道你在product_subcategory字段中有逗号分隔的值,并且你想获得具有通过其传递的特定id的行您SQL查询的表单。

因此,您需要使用\yii\db\Expression子句中的where,如下所示。

\backend\models\Product::find()
->where(['product_category' => $data["key1"]])
->andWhere(new \yii\db\Expression('FIND_IN_SET(:cat_to_find,product_subcategory)'))
->andWhere(['company' => $data["key3"]])
->addParams([':cat_to_find' => $data["key2"]])
->one();

你的行动应该是这样的

public function actionP1()
    {
        $data2 = Yii::$app->request->post('result');
        $data    = $_POST["result"];
        $data    = json_decode("$data", true);
        if (isset($data)) {
            $test = $data;
            $modelfedbkshiprate = \backend\models\Product::find()->where(['product_category' => $data["key1"]])->andWhere(new \yii\db\Expression('FIND_IN_SET(:cat_to_find,product_subcategory)'))->andWhere(['company' => $data["key3"]])->addParams([':cat_to_find' => $data["key2"]])->one();
        } else {
            $test = "Ajax failed";
        }
        return \yii\helpers\Json::encode($modelfedbkshiprate);          
    }

答案 1 :(得分:0)

关注此(Find_In_SET)对我有用。

Categories::find()->where(new Expression('FIND_IN_SET(:category_to_find, categories)'))->addParams([':category_to_find' => 3])->asArray()->all();