【问题标题】:Extendable models in LaravelLaravel 中的可扩展模型
【发布时间】:2015-07-13 10:33:46
【问题描述】:

我正在尝试以这样一种方式构建我的模型,即始终有一个 baseUser 具有每个用户需要的基本功能。

然后在环境 a) 中,我可能想以不同于环境 b) 的方式扩展此用户。

我的基类:

<?php
namespace App\Libraries;

use Illuminate\Database\Eloquent\Model;

abstract class basicUser extends model
{
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = array('name', 'email');

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('remember_token', 'password');

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $guarded = array();

    public function ratings()
    {
        return $this->hasMany('\App\Rating');
    }


    /*==========  Override class variable declarations for children.  ==========*/

    protected function setFillableAttribute($value)
    {
        if (count(class_parents($this)) > 1) # Check if there is more than one parent, they all need the Eloquent model.
        {
            # Yes, there are parents. We need to combine the parent $fillable with the extra ones in the child.
            $this->fillable = array_unique(array_merge($this->fillable,$value));
        }
        else
        {
            # No parents except Eloquent model.
            $this->fillable = $value;
        }
    }

    protected function setHiddenAttribute($value)
    {
        if (count(class_parents($this)) > 1) # Check if there is more than one parent, they all need the Eloquent model.
        {
            # Yes, there are parents. We need to combine the parent $hidden with the extra ones in the child.
            $this->hidden = array_unique(array_merge($this->hidden,$value));
        }
        else
        {
            # No parents except Eloquent model.
            $this->hidden = $value;
        }
    }

    protected function setGuardedAttribute($value)
    {
        if (count(class_parents($this)) > 1) # Check if there is more than one parent, they all need the Eloquent model.
        {
            # Yes, there are parents. We need to combine the parent $guarded with the extra ones in the child.
            $this->guarded = array_unique(array_merge($this->guarded,$value));
        }
        else
        {
            # No parents except Eloquent model.
            $this->guarded = $value;
        }
    }
}

我的基类中的 set...Attribute() 方法背后的想法是,如果孩子想要添加到 $fillable、$hidden 或 $guarded,则不会重置父值。

<?php
namespace App\Libraries;

use App\Libraries\basicUser;

class User extends  basicUser
{
    protected $fillable = array('extra');

    protected $hidden = array(); 

    protected $guarded = array();
}

现在我真正想要的是让我的$u = user::find(1); 拥有密码和remember_token 被隐藏......但受保护的$hidden 似乎被覆盖了。

这种结构甚至可能吗?我是否以错误的方式接近它?

【问题讨论】:

    标签: php laravel laravel-5 eloquent


    【解决方案1】:

    $hidden 被覆盖,因为您在子类中重新声明了此属性。从 User 类中删除以下内容,它应该足以使其工作:

    protected $hidden = array(); 
    

    【讨论】:

    • 重点是我希望能够通过在 User 类中声明 $hidden 来添加到现有数组中。我可以做类似的事情:protected $hidden_child; $this-&gt;hidden = array_merge($this-&gt;hidden,$this-&gt;hidden_child); 但我想通过重新声明来做到这一点(这就是为什么我想到了父类中的 set...Attribute() 方法。)
    • setHiddenAttribute 只有在您尝试分配给 $object->hidden 时没有公开的 $hidden 属性时才会被调用。在“protected $hidden = array();”中声明变量时不会调用它因为 PHP 的 __set() 没有被调用,这就是所有 setXattribute 魔法
    • 如果要扩展 $hidden 数组,需要在子类构造函数中进行。您需要使用 array_merge($this->hidden, $someOtherArray) 或显式调用 setHiddenAttribute 因为这里子模型可以访问 $hidden 并且 __set 仍然不会被调用
    • 所以基本上没有办法做到这一点,除了从父级+新值重新声明完整?
    • 不可能按照您想要的方式进行,但是有不同的方法可以做到这一点。在构造函数中扩展数组,覆盖 getHidden() 方法并在那里扩展列表,...
    猜你喜欢
    • 1970-01-01
    • 2020-06-19
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2021-01-26
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多