【问题标题】:Laravel Custom Model Class - Field Does not have a Default ValueLaravel 自定义模型类 - 字段没有默认值
【发布时间】:2018-08-14 04:20:15
【问题描述】:

所以,我有一个名为 RecursiveModel 的自定义 Model 扩展类:

use Illuminate\Database\Eloquent\Model;
use ... RecursiveHelper;

class RecursiveModel extends Model {
    private $recursiveHelper = null;

    public function __construct(){
        $this->recursiveHelper = new RecursiveHelper();

        parent::__construct();
    }

    public function save(array $options = []){
        parent::save($options);
    }

    ...

    // Additional methods available for Recursive Models (self-referenced `parent_id` relationships)
}

还有一个 Model 扩展了这个 RecursiveModel 类而不是基类 Model

use ... RecursiveModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class Line extends RecursiveModel {

    use SoftDeletes;

    protected $table = "lines";
    protected $primaryKey = "id";

    public function parent(){
        return $this->belongsTo(self::class, "parent_id", "id");
    }

    public function children(){
        return $this->hasMany(self::class, "parent_id", "id");
    }
}

一切都很好,并且使用以前导入的记录(回到 Line 扩展 Model 而不是 RecursiveModel 时,我能够毫无问题地使用我的 RecursiveHelper 方法/逻辑。现在,我试图刷新我的数据库,它调用了Seeder

use Illuminate\Database\Seeder;
use ... Slugger;
use ... Line;

class LinesSeeder extends Seeder {
    public function run(){
        $parentLine = Line::create([
            "name" => "Line Item",
            "slug" => $this->slugger->slugify("Line Item"),
            "created_at" => date("Y-m-d H:i:s"),
            "updated_at" => date("Y-m-d H:i:s"),
        ]);

        $childLine = Line::create([
            "name" => "Child Line Item",
            "slug" => $this->slugger->slugify("Child Line Item"),
            "parent_id" => $parentLine->id,
            "created_at" => date("Y-m-d H:i:s"),
            "updated_at" => date("Y-m-d H:i:s"),
        ]);

        ... 
    }
}

如前所述,当 Line 扩展 Model 而不是 RecursiveModel 时,此代码可以正常工作。但是现在,我遇到了这个错误:

SQLSTATE[HY000]:一般错误:1364 字段“名称”没有默认值(SQL:插入lines
(updated_at, created_at) 值 (2018-08-13 15:56:45, 2018-08-13 15:56:45))

Line::create([...]); 似乎没有接收到传递的参数;扩展Model.php 时有什么遗漏吗?我试过添加:

public function create(array $options = []){
    parent::create($options);
}

RecursiveModel,但这只会引发另一个错误(我不认为create() 方法是Model.php 的一部分,而是Builder.php。)

另外,protected $fillable 不是问题,在我的mysql 连接上设置'strict' => true, 也不是问题;已经尝试了这两种方法都无济于事。

按照建议,将RecursiveModel__construct 方法更新为:

public function __construct(array $attributes = []){
    $this->recursiveHelper = new RecursiveHelper();

    return parent::__construct($attributes);
}

不幸的是,仍然出现同样的错误。

编辑:Line.php 有一个 __construct 方法,从我逐个模型应用 $this->recursiveHelper 时继承而来;解决方案是更新签名以匹配(如上所述)或从扩展模型中删除 __construct

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    模型构造函数需要接受一个属性数组:

    public function __construct(array $attributes = [])
    

    【讨论】:

    • 哦 nvm;将其添加到Line.php;现在它可以工作了。将更新我的代码并关闭它。谢谢! (或者,从Line.php 中删除__construct,因为我没有覆盖任何东西)
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 2017-08-28
    • 2012-09-25
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    相关资源
    最近更新 更多