【问题标题】:Eloquent relationship and class with constructor雄辩的关系和类与构造函数
【发布时间】:2019-02-16 21:01:30
【问题描述】:

我有两个通过 hasMany 和 belongsTo 方法连接的类。

class InquiryParameter extends Model
{
    public function translations()
    {
        return $this->hasMany(InquiryParameterTranslation::class);
    }
}

class InquiryParameterTranslation extends Model
{
    public function __construct($inquiry_parameter_id, $language_code, $name, $description)
    {
            $this->inquiry_parameter_id = $inquiry_parameter_id;
            $this->language_code = $language_code;
            $this->name = $name;
            $this->description = $description;
    }
}

但是,当我创建新对象时

$inquiry_parameter = new InquiryParameter;

然后调用方法翻译。

$names = $inquiry_parameter->translations;

我收到错误:

类型错误:函数的参数太少 App\InquiryParameterTranslation::__construct(), 0 传入 /Users/SouL-MAC/Code/konfig/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php 在第 653 行,预计正好 4 行(查看: /Users/SouL-MAC/Code/konfig/resources/views/admin/inquiry/parameters.blade.php)

是否可以与包含构造函数的类使用雄辩的关系?还是我做错了什么?

感谢您的回复

【问题讨论】:

  • 我认为你的模型构造函数中需要parent::__construct(...)
  • 顺便说一句:这似乎是错误的:$inquiry_parameter = new InquiryParameter;。应该是这样的:$inquiry_parameter = new InquiryParameter($inquiry_parameter_id, $language_code, $name, $description);
  • InquiryParameterTranslation,我还没有 InquiryParameter 类的构造函数
  • 请查看我的回答:stackoverflow.com/a/52293260/5013099

标签: php laravel relationship laravel-5.5 has-many


【解决方案1】:

$names = $inquiry_parameter->翻译;

上述代码运行时,实际上是创建了一个新的 Class InquiryParameterTranslation 对象,而没有向构造函数传递任何参数。但是您的构造函数需要参数。因此它会导致错误。

解决这个问题的方法是改变你的构造函数代码如下:

public function __construct()
{
    // no parameter in constructor
}

然后创建另一个函数(如下所示)来初始化模型属性

public function initialize($inquiry_parameter_id, $language_code, $name, $description)
{
        $this->inquiry_parameter_id = $inquiry_parameter_id;
        $this->language_code = $language_code;
        $this->name = $name;
        $this->description = $description;
}

通过进行上述更改,您的代码将运行良好,当您需要向数据库添加新翻译时,您可以使用以下代码(示例)

$translation = new InquiryParameterTranslation;
$translation->initialize($inquiry_parameter_id, $language_code, $name, $description);

$translation->save();

【讨论】:

  • 就是这样,我就是这样做的,但我希望我可以使用构造函数本身。所以我认为,不可能使用具有雄辩关系的构造函数。谢谢。
  • 但是如果我想使用构造函数来创建模型对象,这在使用关系时是不可能的吗?
【解决方案2】:

由于您正在扩展已经具有构造函数的给定对象,因此您需要使用相应的属性来调用它。 请参阅Illuminate\Database\Eloquent\Model 的 API

试试这样的:

public function __construct(array $attributes = array())
{
        parent::__construct($attributes);
}

在您的特定情况下,以下将起作用:

public function __construct($inquiry_parameter_id, $language_code, $name, $description)
{
        parent::__construct();

        $this->inquiry_parameter_id = $inquiry_parameter_id;
        $this->language_code = $language_code;
        $this->name = $name;
        $this->description = $description;
}

【讨论】:

  • 我把构造函数放到模型类和父类::__construct();到 InquiryParameterTranslation 构造函数,和前面的一样
猜你喜欢
  • 1970-01-01
  • 2017-05-19
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 2016-05-14
相关资源
最近更新 更多