fastadmin關(guān)聯(lián)字段的顯示及篩選

當(dāng)在使用fastadmin快速開發(fā)項目時,往往需要關(guān)聯(lián)數(shù)據(jù)表。但是在關(guān)聯(lián)數(shù)據(jù)后進行篩選時,經(jīng)常出現(xiàn)字段沖突的問題,所以封裝了個小方法解決字段沖突。

封裝一個修改字段的方法

    
    /*
    * 修改篩選請求字段名稱
    * @param String $old 請求的字段名稱
    * @param String $new 需要修改的字段名稱
    */
    public function changeName($old, $new)
    {
        $f = json_decode($_GET['filter'], true);
        // @符屏蔽錯誤,偷懶沒寫驗證
        @$s = $f[$old];

        $res = [];
        if ($s != '') {
            unset($f[$old]);
            $f[$new] = $s;
            $_GET['filter'] = json_encode($f);
        }

        $res["filter"] = json_encode($f);

        $f = json_decode($_GET['op'], true);
        @$s = $f[$old];

        if ($s != '') {
            unset($f[$old]);
            $f[$new] = $s;
            $_GET['op'] = json_encode($f);
        }

        $res["op"] = json_encode($f);
        $this->request->get($res);
    }

修改application/common/controller/Backend.php中426行左右的地方(因為修改字段名稱的思路中添加了表名,所以在篩選日期的時候[比如通用字段createtime],這個地方的分割會出現(xiàn)錯誤)

    if ($arr[0] === '') {
        $sym = $sym == 'RANGE' ? '<=' : '>';
        $arr = $arr[1];
    } elseif ($arr[1] === '') {
        $sym = $sym == 'RANGE' ? '>=' : '<';
        $arr = $arr[0];
    }
    $tableArr = explode('.', $k);
    // 以上是自帶的代碼
    // 這里添加一個處理
    if (count($tableArr) > 1) {
        $tableArr[0] = $tableArr[1];
        unset($tableArr[1]);
    }
    //結(jié)束
    //以下是自帶的代碼
    if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
        //修復(fù)關(guān)聯(lián)模型下時間無法搜索的BUG
        $relation = Loader::parseName($tableArr[0], 1, false);
        $alias[$this->model->$relation()->getTable()] = $tableArr[0];
    }

復(fù)制 application/admin/library/traits/Backend.php 中的index方法到你的控制器,然后改寫它:

    public function index()
    {
        //設(shè)置過濾方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果發(fā)送的來源是Selectpage,則轉(zhuǎn)發(fā)到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            
            // 此處將篩選請求中有爭議的字段"name"指定為tableA中的name
            $this->changeName("name", "a.name");

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            // 假設(shè)tableA 和 tableB 中都有個name字段,tableA中的b_id關(guān)聯(lián)著tableB的主鍵id
            $list = $this->model
                ->alias("a")
                 // TP的關(guān)聯(lián)查詢
                ->join("tableB b", "a.b_id = b.id", "left")
                ->where($where)
                ->order($sort, $order)
                // 此處的bname渲染到j(luò)s中
                ->field("a.* , b.name as bname")
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

對應(yīng)的js中將字段渲染出來

...
// 生成的字段
// {field: 'b_id', title: __('b_id')},

// 修改為:
{field: 'bname', title: __('b_id')},
...

以上為自己慢慢研究出來的,雖然有些不規(guī)范,但是總算是解決了問題,用來快速開發(fā)外包小項目足夠用了!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容