php/laravel不确定调用什么代码来创建模型的新实例
我对php比较陌生。在我的项目中,正在创建一个模型实例,如下所示:php/laravel不确定调用什么代码来创建模型的新实例,php,laravel,constructor,model,Php,Laravel,Constructor,Model,我对php比较陌生。在我的项目中,正在创建一个模型实例,如下所示: $blah = new SomeModelName(['arg1' => $arg1, 'arg2' => $arg2, 'arg3' => $arg3]); 我认为当使用new关键字调用模型中的\uu costruct函数时。好的,该模型将不同的类一直扩展到model.php,它位于\illumb\Database\elount中 我追溯到了基本的Model.php类,在所有的子类中都没有覆盖基本Model
$blah = new SomeModelName(['arg1' => $arg1, 'arg2' => $arg2, 'arg3' => $arg3]);
我认为当使用new
关键字调用模型中的\uu costruct
函数时。好的,该模型将不同的类一直扩展到model.php
,它位于\illumb\Database\elount
中
我追溯到了基本的Model.php
类,在所有的子类中都没有覆盖基本Model.php
类中的\uuu构造的
函数,因此,我认为应该调用\illighted\Database\elount
中的baseModel.php
类中的\construct
函数。但是,我在baseModel.php
类中放了一条调试语句,它没有输出,因此在baseModel.php
中似乎有\uu构造
。php不是被调用的\uu构造
函数
有人能告诉我在初始化类时在哪里可以找到调用的
\u构造函数吗 用户定义的模型继承自illumb\Database\Eloquent\Model
,后者是定义此方法的抽象类:
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = [])
{
$this->bootIfNotBooted();
$this->initializeTraits();
$this->syncOriginal();
$this->fill($attributes);
}
请注意,参数的类型提示为数组
。如果将函数传递给其他对象,则会出现TypeError
,并且不会运行该函数
您可以在自己的模型上创建一个构造函数,该构造函数使用提供的参数执行某些操作。例如:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Widget extends Model
{
private $foo;
private $bar;
public function __construct($arg1, $arg2)
{
$this->foo = $arg1;
$this->bar = $arg2;
parent::__construct();
}
}
$widget = new Widget($arg1, $arg2);
我不知道你想干什么。但是当你看这个类的时候,你会看到神奇的方法。下面是在构造函数中发生的情况
public function __construct(array $attributes = [])
{
$this->bootIfNotBooted();
$this->initializeTraits();
$this->syncOriginal();
$this->fill($attributes);
}
你看有一个参数。它将帮助您启动具有质量分配的对象(但您必须先编写$filleble
属性)
因此,如果您想创建一个新对象,只需编写新公司(['name'=>“Hilman Corp.”,'city'=>“Mojokerto”])
。这是公司
课程
class Company extends Model
{
protected $fillable = ['name','city'];
}
也许您想将逻辑添加到构造函数中,并将其添加到大写首字母中。您可以按如下方式轻松修改代码
class Company extends Model
{
protected $fillable = ['name','city'];
public function __construct(array $attributes=[])
{
$attributes['city'] = ucfirst($attributes['city']);
parent::__construct($attributes);
}
}
我的建议是不要重写一些可能会破坏拉威尔对待一个类的方式的东西。在以后升级Laravel版本时,这可能会给您带来痛苦。除非您定义了自己的构造函数来使用参数,否则传递参数是没有意义的。@miken32这就是我感到困惑的原因SomeModelName.php
没有自己的\u构造
函数,它扩展了SomeOtherModel.php
,而SomeOtherModel.php也没有\u构造
函数。依此类推回到Model.php
,这是我唯一能找到\u构造
函数的地方。所以我想我是在问,如果我找不到一个\uuuu构造
函数来覆盖基础Model.php
类中的\uu构造
函数,我还应该在哪里查看运行new SomeModelName
时发生了什么?调用new SomeModel()
时称为\uu构造()
函数定义在illumb\Database\elount\Model
上,它不会在任何地方被覆盖。参数是属性数组。@dparoli是的,我知道应该这样做。但是,我在baseModel.php
中的\uu construct
函数中放了一个display语句,而该display语句没有显示,这使我相信baseModel.php
类中的\uu construct
函数没有被调用,因此我的问题是,如果你给它传递了错误的参数,这个函数就不会运行。检查日志中的“TypeError:传递给Illumb/Database/Eloquent/Model::_construct()的参数1必须是数组类型,xxxx给定的”抱歉,我在我的原始帖子中出错了,传递给构造函数的参数实际上是数组,对于构造函数来说应该是合适的。我已经相应地修改了我的OP。但是,即使考虑到我的display语句仍然没有像它应该显示的那样显示。好的,但是看看构造函数对数组做了什么。将其传递到填充模型的fill()
,将属性映射到您没有的数组键。请尝试使用我的示例中的关联数组。你说得对。在我再次检查之后,它是一个关联数组,被传递到构造函数中。很抱歉造成混淆,我已再次更新我的OP以反映这一点。请尝试注释掉这一行$this->fill($attributes)在雄辩的/模型构造函数中编写>并查看派生类是否分配了属性,您将看到该类被调用。@GharbadTheWeak或just putdd($attributes)代码>作为第一行。
class Company extends Model
{
protected $fillable = ['name','city'];
public function __construct(array $attributes=[])
{
$attributes['city'] = ucfirst($attributes['city']);
parent::__construct($attributes);
}
}