2015-07-13 45 views
1

我試圖以這樣一種方式構建我的模型,即始終有一個baseUser具有每個用戶所需的基本功能。Laravel中的可擴展模型

然後在環境a)我可能希望tot以另一種方式擴展此用戶,而不是在環境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; 
     } 
    } 
} 

背後的集...屬性()在我的基類方法的想法是,如果一個孩子想加入到$可填寫,$隱藏或$森嚴,該父值不重置。

<?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被隱藏...但保護$隱藏似乎被覆蓋。

這種結構甚至可能嗎?我是否以錯誤的方式接近它?

回答

1

$ hidden被覆蓋,因爲您在子類中重新聲明瞭此屬性。取出從用戶以下類,它應該足以使其工作:

protected $hidden = array(); 
+0

的一點是,我希望能夠通過剛剛宣佈$隱藏在用戶類添加到現有陣列。我可以像這樣做: 'protected $ hidden_​​child;我想這樣做只是通過redeclaring(這就是爲什麼我想到的設置...屬性(這就是爲什麼我想這個設置...屬性(隱藏,$ this-> hidden_​​child); $ this-> hidden = array_merge($ this-> hidden,$ this-> hidden_​​child) )方法。 – user2693053

+0

setHiddenAttribute只會在您試圖賦予$ object-> hidden時沒有公共$ hidden atrribute時被調用。當你在「protected $ hidden = array();」中聲明一個變量時,它不會被調用。因爲PHP的__set()沒有被調用,那麼這就是所有的setXattribute的魔術 –

+1

如果你想擴展$ hidden數組,你需要在子類構造函數中做到這一點。你需要使用array_merge($ this-> hidden,$ someOtherArray)或者明確地調用setHiddenAttribute,因爲這裏的子模型有權訪問$ hidden並且__set仍然不會被調用 –