dongwei1855 2019-07-11 19:45
浏览 84
已采纳

如何将属性传递回父类构造函数方法

How can I pass variables from a child class into the methods of a parent constructors class? I am able to create an asset with this constructor without any problems.

I have a feeling that this has something to do with the way that eloquent is handling the protected videos. All three of the variables are making it to their respective methods before the error is being thrown.


//Parent Class
class Asset extends Model
{
    protected $fillable = [
        'type', 'title','origin',
    ];


    // protected $author_id;
    // protected $keywords = [];
    // protected $proof_status;
    // protected $description;
    // protected $usageHistory;

    public function __construct($type, $title, $origin)
    {
        $this->setType($type);
        $this->setTitle($title);
        $this->setOrigin($origin);
    }

    // Setters
    public function setType($type){
        $this->type = $type;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function setOrigin($origin)
    {
        $this->origin = $origin;
    }

    public function setAltTitle($alt_title)
    {
        $this->alt_title = $alt_title;
    }

    public function setDescription($description)
    {
        $this->description = $description;
    }
}

// Child Class
class Recipe extends Asset
{
    public function __construct($type, $title, $origin){
        parent::__construct($type, $title, $origin);

    }
}

I expected this to set $type, $origin, and $title but instead this is the error that I receive SQLSTATE[42S22]: Column not found: 1054 Unknown column 'type' in 'field list' (SQL: insert into `recipes` (`type`, `updated_at`, `created_at`) values (recipe, 2019-07-11 19:35:23, 2019-07-11 19:35:23))

Asset:DB

  • 写回答

1条回答 默认 最新

  • duanbing8817 2019-07-11 20:11
    关注

    You can resolve this issue by adding the following I believe:

    // Child Class
    class Recipe extends Asset
    {
        protected $table = 'assets';
    }
    

    However, I would highly recommend examining if what you are doing is an anti-pattern. Without knowing more information it sounds like in the long run you are going to want to create polymorphic relations on the assets table with foreign keys to other tables.

    I would strongly consider not implementing the code above to resolve your issue. Using this will most likely continue to cause headaches going forward.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?