Skip to content

使用视图模型viewMapping或autoMapping关联属性直接报错Cannot assign null to property #815

@lg19881006

Description

@lg19881006

打印测试代码

Route::get('test', function () {
    $data = \app\entity\Orders::find(84);
    dump($data);
});

报错信息

Cannot assign null to property app\entity\Orders::$mstatus of type string
<?php
// +----------------------------------------------------------------------
// | 商户模型
// +----------------------------------------------------------------------
namespace app\model;

use think\Model;

class Merchants extends Model
{
}
<?php
// +----------------------------------------------------------------------
// | 订单模型
// +----------------------------------------------------------------------
namespace app\model;

use think\Model;
use think\model\relation\BelongsTo;

class Orders extends Model
{

    /**
     * 关联商户
     */
    public function merchant(): BelongsTo
    {
        return $this->belongsTo(Merchants::class, 'merchant_id', 'id');
    }

}
<?php
// +----------------------------------------------------------------------
// | 商户视图模型
// +----------------------------------------------------------------------
namespace app\entity;

use think\model\View;

class Merchants extends View
{


    public string $id;
    public string $status;

}
<?php
// +----------------------------------------------------------------------
// | 订单视图模型
// +----------------------------------------------------------------------
namespace app\entity;

use think\model\View;

class Orders extends View
{

    public string $id;
    public string $merchant_id;
    public string $amount;
    public string $mstatus;

    public function getOptions():array
    {
        return [
            //'autoMapping' => ['merchant']
            'viewMapping' => ['mstatus' => 'merchant->status']
        ];
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions