ThinkPHP5.1中數據查詢使用field方法數組參數起別名時遇到的問題


首先數據庫基本查詢是沒有問題的

<?php

namespace app\index\controller;
use think\Db;

class Demo5
{
    //1.單條查詢
    public function find()
    {
        $res = Db::table('customers')
            ->field('Name,CustomerID')
            ->where('CustomerID', '=', 1)
            ->find();
        dump(is_null($res) ? '沒有找到' : $res);
    }

}

 返回結果為:

但是當field參數為數組,需要給字段起別名時:

<?php


namespace app\index\controller;
use think\Db;

class Demo5
{
    //1.單條查詢
    public function find()
    {
        $res = Db::table('customers')
//            ->field('Name,CustomerID')
            ->field(['CustomerID'=>'顧客編號'])
            ->where('CustomerID', '=', 1)
            ->find();
        dump(is_null($res) ? '沒有找到' : $res);
    }

}

 卻報了以下錯誤:

(修改字段為中文時才會出現該錯誤,小白看不懂報錯...)查詢手冊發現,還有另外一種起別名的方法:

<?php


namespace app\index\controller;
use think\Db;

class Demo5
{
    //1.單條查詢
    public function find()
    {
        $res = Db::table('customers')
//            ->field('Name,CustomerID')
//            ->field(['CustomerID'=>'顧客編號'])
            ->field('CustomerID as 顧客編號')
            ->where('CustomerID', '=', 1)
            ->find();
        dump(is_null($res) ? '沒有找到' : $res);
    }

}

結果成功運行:

不禁好奇:是不支持數組參數起別名了嗎?還是說我哪里配置的不正確?

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM