laravel 扩展 Eloquent 的类的构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16888698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Constructors on classes extending Eloquent
提问by vikingsfan19
I just started a new website and I wanted to make use of Eloquent. In the process of seeding my database, I noticed that I would get empty rows added if I had included any kind of constructor on the model that extends eloquent. For example, running this seeder:
我刚刚创建了一个新网站,我想使用 Eloquent。在为我的数据库做种的过程中,我注意到如果我在扩展 eloquent 的模型上包含任何类型的构造函数,我会添加空行。例如,运行这个播种机:
<?php
class TeamTableSeeder extends Seeder {
public function run()
{
DB::table('tm_team')->delete();
Team::create(array(
'city' => 'Minneapolis',
'state' => 'MN',
'country' => 'USA',
'name' => 'Twins'
)
);
Team::create(array(
'city' => 'Detroit',
'state' => 'MI',
'country' => 'USA',
'name' => 'Tigers'
)
);
}
}
With this as my Team class:
以此作为我的团队课程:
<?php
class Team extends Eloquent {
protected $table = 'tm_team';
protected $primaryKey = 'team_id';
public function Team(){
// null
}
}
Yields this:
产生这个:
team_id | city | state | country | name | created_at | updated_at | deleted_at
1 | | | | | 2013-06-02 00:29:31 | 2013-06-02 00:29:31 | NULL
2 | | | | | 2013-06-02 00:29:31 | 2013-06-02 00:29:31 | NULL
Simply removing the constructor all together allows the seeder to work as expected. What exactly am I doing wrong with the constructor?
简单地一起删除构造函数允许播种机按预期工作。我到底在构造函数上做错了什么?
回答by Jan P.
You have to call parent::__construct
to make things work here, if you look at the constructor of the Eloquent
class:
parent::__construct
如果您查看Eloquent
类的构造函数,则必须调用以使事情在这里工作:
public function __construct(array $attributes = array())
{
if ( ! isset(static::$booted[get_class($this)]))
{
static::boot();
static::$booted[get_class($this)] = true;
}
$this->fill($attributes);
}
The boot
method is called and the booted
property is set. I don't really know what this is doing but depending on your problem it seems relevant :P
该boot
方法被称为和booted
属性设置。我真的不知道这是在做什么,但根据您的问题,它似乎相关:P
Refactor your constructor to get the attributes
array and put it to the parent constructor.
重构您的构造函数以获取attributes
数组并将其放入父构造函数。
Update
更新
Here is the needed code:
这是所需的代码:
class MyModel extends Eloquent {
public function __construct($attributes = array()) {
parent::__construct($attributes); // Eloquent
// Your construct code.
}
}
回答by Chemaclass
In laravel 3 you must put the second parameter '$exists' with default value "false".
在 Laravel 3 中,您必须将第二个参数 '$exists' 设置为默认值“false”。
class Model extends Eloquent {
public function __construct($attr = array(), $exists = false) {
parent::__construct($attr, $exists);
//other sentences...
}
}
回答by overcomer
You can use this generic method that allows you to pass a parameter too.
您也可以使用这种允许您传递参数的通用方法。
/**
* Overload model constructor.
*
* $value sets a Team's value (Optional)
*/
public function __construct($value = null, array $attributes = array())
{
parent::__construct($attributes);
$this->value = $value;
// Do other staff...
}