【问题标题】:Constructors on classes extending Eloquent扩展 Eloquent 的类的构造函数
【发布时间】:2013-05-29 03:06:08
【问题描述】:

我刚刚创建了一个新网站,我想使用 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'
            )
        );
    }

}

将此作为我的团队课程:

<?php

class Team extends Eloquent {

    protected $table = 'tm_team';
    protected $primaryKey = 'team_id';

    public function Team(){
        // null
    }
}

产生这个:

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

只需将构造函数全部移除,播种器就可以按预期工作。构造函数到底做错了什么?

【问题讨论】:

  • 因为 Eloquent 有它自己的构造函数,并且你正在做的事情取消了 eloquent 需要工作的所有操作。

标签: php laravel laravel-4 eloquent seeding


【解决方案1】:

你也可以使用这个通用的方法来传递参数。

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

【讨论】:

    【解决方案2】:

    在 laravel 3 中,您必须将第二个参数 '$exists' 设置为默认值 “假”。

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

    【讨论】:

      【解决方案3】:

      如果您查看Eloquent 类的构造函数,您必须调用parent::__construct 才能在此处工作:

      public function __construct(array $attributes = array())
      {
          if ( ! isset(static::$booted[get_class($this)]))
          {
              static::boot();
      
              static::$booted[get_class($this)] = true;
          }
      
          $this->fill($attributes);
      }
      

      调用boot 方法并设置booted 属性。我真的不知道这是在做什么,但根据您的问题,这似乎是相关的:P

      重构您的构造函数以获取 attributes 数组并将其放入父构造函数。

      更新

      这是所需的代码:

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

      【讨论】:

      • +1 用于更新,但 -1 用于第一个代码。原因:可重用性......如果雄辩的__construct()发生变化,它将不兼容
      • 如果您正确阅读我的答案,您会看到,我只是复制了 Eloquent 构造函数进行演示:)
      • 哦该死的,是的,你是对的。行尾解释和更新提到让我感到困惑。响应中的第一个代码通常是快速复制/粘贴答案。也许你应该用 github 链接替换它(就像我在编辑中所做的那样)。
      • 调用父级构造函数的安全方法是call_user_func_array(array('parent', '__construct'), func_get_args());
      • @wkjagt 但是在这种特定情况下,您将如何定义新构造函数的预期参数?
      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 2013-11-15
      • 2019-01-07
      • 2011-07-28
      • 2014-04-18
      • 2012-02-28
      相关资源
      最近更新 更多