代码之家  ›  专栏  ›  技术社区  ›  Tim Lewis

Laravel自定义模型类-字段没有默认值

  •  1
  • Tim Lewis  · 技术社区  · 6 年前

    所以,我有一个习惯 Model 调用的扩展类 RecursiveModel :

    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)
    }
    

    模型 递归模型 类而不是基 模型 班级:

    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 扩展的 而不是 递归模型 ,我可以使用 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"),
            ]);
    
            ... 
        }
    }
    

    如前所述,当 生产线 模型 而不是 ,此代码可以正常工作。但现在,我遇到了一个错误:

    SQLSTATE[HY000]:一般错误:1364字段“name”没有默认值(SQL:insert-into 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);
    }
    

    ,但这又是一个错误(我不认为 create() 方法是 ,但是 Builder.php .)

    而且,这与 protected $fillable ,设置也不是问题 'strict' => true, 关于我的 mysql 联系;已经尝试过这两种方法都没有效果。

    如建议,更新 __construct 递归模型 致:

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

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

    Line.php 有一个 我申请的时候继承下来的方法 $this->recursiveHelper 逐个模型;解决方案是更新签名以匹配(如上所述)或删除 __构造 从扩展模型开始。

    1 回复  |  直到 6 年前
        1
  •  3
  •   lagbox    6 年前

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

    public function __construct(array $attributes = [])