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.

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

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab