Laravel6.0 Eloquent无法获取特定的列名值

时间:2019-11-19 08:20:49

标签: php laravel eloquent

我正在尝试将系统从Laravel4.2更新为Laravel6.0。 我们的数据库是AWS AuroraPostgreSQL10.7

我使用Eloquent来获取表记录。 当我运行此命令时:

$obj = self::find($id);

特定列“开始”和“结束”始终为NULL。 我们的旧系统(Laravel4.2)没有这个问题。

问题出在哪里? 谢谢


[添加20-11-2019] 我的表结构在这里。

CREATE TABLE public.sub_markets (
    id serial NOT NULL,
    market_id int4 NOT NULL,
    parent_id int4 NOT NULL DEFAULT '-1'::integer,
    team_id int4 NOT NULL DEFAULT '-1'::integer,
    "type" int2 NOT NULL DEFAULT 0::smallint,
    area_size float8 NOT NULL DEFAULT 0::double precision,
    exist_family_count int4 NOT NULL DEFAULT 0,
    initial_blank_count int4 NOT NULL DEFAULT 0,
    blank_count int4 NOT NULL DEFAULT 0,
    initial_unmatch_count int4 NOT NULL DEFAULT 0,
    unmatch_count int4 NOT NULL DEFAULT 0,
    initial_share_unmatch_count int4 NOT NULL DEFAULT 0,
    share_unmatch_count int4 NOT NULL DEFAULT 0,
    contract_count int4 NOT NULL DEFAULT 0,
    reserve_count int4 NOT NULL DEFAULT 0,
    fulltalk_count int4 NOT NULL DEFAULT 0,
    revisit_count int4 NOT NULL DEFAULT 0,
    absence_count int4 NOT NULL DEFAULT 0,
    rumor_count int4 NOT NULL DEFAULT 0,
    ifo_count int4 NOT NULL DEFAULT 0,
    fo_count int4 NOT NULL DEFAULT 0,
    ban_count int4 NOT NULL DEFAULT 0,
    exempt_count int4 NOT NULL DEFAULT 0,
    exempt2_count int4 NOT NULL DEFAULT 0,
    co_count int4 NOT NULL DEFAULT 0,
    is_area_lock int2 NOT NULL DEFAULT 0::smallint,
    created_at timestamp NOT NULL DEFAULT now(),
    updated_at timestamp NOT NULL DEFAULT now(),
    deleted_at timestamp NULL DEFAULT NULL::timestamp without time zone,
    "start" timestamp NULL DEFAULT NULL::timestamp without time zone,
    "end" timestamp NULL DEFAULT NULL::timestamp without time zone,
    polygon text NULL,
    classroom_body text NULL,
    CONSTRAINT sub_markets_pkey PRIMARY KEY (id)
);

我定义了这个模型类

<?php

namespace App\Markets;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\SoftDeletes;
use Log;

use App\Markets\SubMarketUser;

// SubMarket情報
class SubMarket extends MarketBase
{
    use SoftDeletes;
    protected $table = 'sub_markets';
...
    public static function findById($id)
    {
        $obj = self::find($id);
        Log::debug(__METHOD__,['obj'=>$obj]);
        return $obj;
    }
...
}

我执行此功能,获取此日志。

  

[2019-11-20 10:23:25]本地。调试:App \ Markets \ SubMarket :: findById {“ obj”:{“ App \ Markets \ SubMarket”:{“ id”:246,“ market_id “:258,” parent_id“:-1,” team_id“:-1,” type“:1,” area_size“:” 0.17“,” exist_family_count“:155,” initial_blank_count“:0,” blank_count“:0, “ initial_unmatch_count”:0,“ unmatch_count”:4,“ initial_share_unmatch_count”:0,“ share_unmatch_count”:0,“ contract_count”:92,“ reserve_count”:1,“ fulltalk_count”:2,“ revisit_count”:1,“ absence_count “:1,” rumor_count“:2,” ifo_count“:1,” fo_count“:0,” ban_count“:0,” exempt_count“:0,” exempt2_count“:1,” co_count“:3,” is_area_lock“: 0,“ created_at”:“ 2019-11-18 14:22:51”,“ updated_at”:“ 2019-11-19 11:14:43”,“ deleted_at”:null,“ start”:null,“ end “:null,” polygon“:” MULTIPOLYGON([HERE IN POLYGON POINT VALUE]]“,” classroom_body“:”“ {[HERE IN JSON INFOMATION]}”}}}

日志输出"start":null,"end":null,。 但是该记录具有startend的值。

select id,market_id,"start", "end" from sub_markets where id=246;

id |market_id|start              |end                |
---|---------|-------------------|-------------------|
246|      258|2019-11-18 14:22:51|2019-11-18 14:22:51|

为什么?


[添加2019-11-21]

我正在尝试其他测试。

public static function findById($id)
{
    $obj = self::find($id);
    Log::debug(__METHOD__,['obj1'=>$obj->start]);
    $tmp = DB::table('sub_markets')->select('id','start','end')->where('id','=',$id)->first();
    Log::debug(__METHOD__,['tmp1'=>$tmp]);
    $obj->start = Common::utc2Datetime('2019-11-18 11:11:11');
    $obj->end = Common::utc2Datetime('2019-11-18 15:55:55');
    Log::debug(__METHOD__,['obj2'=>$obj->start]);
    $obj->save();
    Log::debug(__METHOD__,['obj3'=>$obj->start]);
    $tmp = DB::table('sub_markets')->select('id','start','end')->where('id','=',$id)->first();
    Log::debug(__METHOD__,['tmp2'=>$tmp]);
    return $obj;
}

执行此代码。输出日志在这里。

  

[2019-11-20 15:14:29]本地。调试:App \ Markets \ SubMarket :: findById {“ obj1”:null}   [2019-11-20 15:14:29] local.DEBUG:App \ Markets \ SubMarket :: findById {“ tmp1”:{“ stdClass”:{“ id”:246,“开始”:“ 2019-11-27 19 10:00:00“,” end“:” 2019-11-19 15:00:00“}}}   [2019-11-20 15:14:29] local.DEBUG:App \ Markets \ SubMarket :: findById {“ obj2”:null}   [2019-11-20 15:14:29] local.DEBUG:App \ Markets \ SubMarket :: findById {“ obj3”:null}   [2019-11-20 15:14:29] local.DEBUG:App \ Markets \ SubMarket :: findById {“ tmp2”:{“ stdClass”:{“ id”:246,“开始”:“ 2019-11-27 18 11:11:11“,” end“:” 2019-11-18 15:55:55“}}}   $ obj-> start始终为空。但是数据库正在更新新时间。

是Laravel6.0的错误吗?


我的问题已经解决。

我重新检查了我们的程序。在MarketBase.php中,此功能。我删除它,开始/结束值就可以得到。

class MarketBase extends Model
{
    use SoftDeletes;
    protected $guarded = array('id');

/*
    public function getStartAttribute()
    {
        return Common::datetime2Utc($this['attributes']['start']);
    }
    public function getEndAttribute()
    {
        return Common::datetime2Utc($this['attributes']['end']);
    }
*/

我不知道原因,但已解决此问题。 谢谢。

0 个答案:

没有答案