vik*_*n19 17 php seeding laravel eloquent laravel-4
我刚开始一个新的网站,我想利用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'
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
以此作为我的团队课程:
<?php
class Team extends Eloquent {
protected $table = 'tm_team';
protected $primaryKey = 'team_id';
public function Team(){
// null
}
}
Run Code Online (Sandbox Code Playgroud)
收益率:
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
Run Code Online (Sandbox Code Playgroud)
只需将构造函数一起移除,就可以使播种机按预期工作.究竟我在构造函数中做错了什么?
Jan*_* P. 26
parent::__construct如果你看一下这个Eloquent类的构造函数,你必须调用make才能在这里工作:
public function __construct(array $attributes = array())
{
if ( ! isset(static::$booted[get_class($this)]))
{
static::boot();
static::$booted[get_class($this)] = true;
}
$this->fill($attributes);
}
Run Code Online (Sandbox Code Playgroud)
boot调用该方法并设置booted属性.我真的不知道这是做什么,但根据你的问题似乎相关:P
重构构造函数以获取attributes数组并将其放到父构造函数中.
更新
这是所需的代码:
class MyModel extends Eloquent {
public function __construct($attributes = array()) {
parent::__construct($attributes); // Eloquent
// Your construct code.
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15925 次 |
| 最近记录: |