Constructors on classes extending Eloquent

依然范特西╮ 提交于 2019-12-20 17:35:43

问题


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:

<?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?


回答1:


You have to call parent::__construct to make things work here, if you look at the constructor of the Eloquent class:

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

Refactor your constructor to get the attributes array and put it to the parent constructor.

Update

Here is the needed code:

class MyModel extends Eloquent {
    public function __construct($attributes = array())  {
        parent::__construct($attributes); // Eloquent
        // Your construct code.
    }
}



回答2:


In laravel 3 you must put the second parameter '$exists' with default value "false".

class Model extends Eloquent {

    public function __construct($attr = array(), $exists = false) {
        parent::__construct($attr, $exists);
       //other sentences...
    }
}



回答3:


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...    
}


来源:https://stackoverflow.com/questions/16888698/constructors-on-classes-extending-eloquent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!