如何使用php查看从数据库到gridview的数据?

时间:2019-03-04 03:12:36

标签: javascript php yii2 vuejs2

我已经遇到相同问题2天了。现在我正在研究如何从数据库中显示多对多关系的值。

现在,我需要从数据库中的帐单表中搜索帐单编号,但我一直坚持显示帐单号中的帐单编号值,收款表中的收款人姓名,组织名称中的值组织表将帐户表中的帐户ID与帐户ID中的帐单表相关,并且需要显示客户名称的全名(姓氏+名字)。

这是我的billsController

public function actionLoadRequest($order_number)
    {
        if (!Yii::$app->request->isAjax) {
            return $this->asJson([
                "status" => false,
                "message" => "Unknown request method.",
                "data" => []
            ]);
        }

        $data = Bill::find()->where(['bill_no'=>$order_number])->one();


        return $this->asJson([
            "status" => true,
            "message" => "",
            "data" => $data
        ]);
    }

这是我的观点

<template v-if="isLoading">
                                                <tr>
                                                    <td colspan="7" class="center aligned">Processing...</td>
                                                </tr>
                                            </template>
                                            <template v-else>
                                                <template v-if="data.length > 0">
                                                    <tr v-for="$bill in data">
                                                        <td>{{bill.bill_no}}</td>
                                                        <td>{{bill.collection}}</td>
                                                        <td>{{bill.organization}}</td>
                                                        <td>{{account.first_name + " " + account.last_name}}</td>
                                                        <td>{{bill.currency + " " +
                                                            bill.Amount}}
                                                        </td>
                                                    </tr>
                                                </template>
                                                <template v-if="data.length === 0">
                                                    <tr>
                                                        <td colspan="7" class="center aligned">No request data.</td>
                                                    </tr>
                                                </template>
                                            </template>

这是我的JavaScript

methods: {
                loadRequest: function () {
                    console.log(this.bill_no);
                    if (this.bill_no === "") {
                        alert("Nothing to search. Please enter bill number.");
                        return;
                    }

                    let self = this;

                    self.isLoading = true;

                    $.ajax({
                        url: "<?= Url::to(["/admin/bills/load-request"]) ?>",
                        method: 'GET',
                        type: 'GET',
                        dataType: 'json',
                        data: {order_number: this.bill_no}
                    }).done(function (data) {
                        if (data.status) {

                            self.data = data.data;

                        } else {
                            alert(data.message);
                            self.data = [];
                        }
                        console.log(data);
                        self.isLoading = false;
                    }).fail(function (jqXHR) {
                        if (jqXHR.responseJSON) {
                            alert("Nothing to search. Please enter bill number.");
                        } else {
                            alert("Nothing to search. Please enter bill number.");
                        }

                        self.isLoading = false;
                    });


                },

            },

编辑-----------------------------

我已经解决了通过更改控制器中的代码来显示数据库中数据的问题

$data = Bill::find()
            ->where(['bill_no'=>$bill_number])
            ->asArray()
            ->all();

但是由于我还是vue js的新手,我的关系出现了问题。

my view

3 个答案:

答案 0 :(得分:0)

我想你需要这样的东西吗?:

$data = Bill::find()->where(['bill_no'=>$order_number])->one();
$account_name = $data->collection->organization->account->name;

显然,托收,组织和帐户是您的帐单模型中建立的关系名称。

希望它能有所帮助。

问候。

答案 1 :(得分:0)

我的建议是使用Pjax。在Pjax中,您具有AJAX事件的历史记录,而无需刷新页面。

同样,您也可以在Action,View和JS文件中使用干净的代码。

您可以在以下链接中看到完整的帮助:Yii2 Pjax Tutorial

答案 2 :(得分:0)

在Bill模型类中,创建一个名为getCollection()的方法,如下所示:

// this function declare a relation named `collection`, notice the C in getCollection is upper case
public function getCollection()
{
    // the `id` is a column in table collection, `collection_id` is a column in table bill, `Collection` is the model class correspond with table collection
    return $this->hasOne(Collection::class, ['id' => 'collection_id']);

    // if you define foreign key in collection table to refer `id` in bill table, use following
    return $this->hasOne(Collection::class, ['bill_id' => 'id']);
}

然后在您的控制器中,添加带有参数with的{​​{1}}函数调用

collection

之后,您将发现$ data ['collection']是您期望的那个集合。 同样,在Bill模型类中添加其他关系,并以您的关系作为参数调用$data = Bill::find() ->where(['bill_no'=>$bill_number]) // there `collection` is the relation you just create, make sure its name is correct, notice the letter case ->with('collection') ->asArray() ->all(); 函数。

请参阅此https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#declaring-relations

希望能帮助您

相关问题