2013-07-25 16 views
2

我使用Redbean作爲我的php應用程序的ORM。不保存php-redbean數據庫的屬性

每個用戶(在這種情況下的員工)必須有密碼才能登錄,我想我會爲他們生成一個密碼,所以他們不必在表單中輸入密碼。

顯然,這意味着它們都必須有一個salt,並且唯一存儲的密碼應該是實際密碼的散列值。因此,我必須將密碼發送給用戶(否則他們不會知道它:D),因此必須將其作爲對象的屬性,而不必將其保存爲數據庫。

一個聰明的地方生成的密碼將在模型中,因此它基本上是由本身會爲每一位新員工(用戶),爲此我創造了這個模式:

class Model_employee extends RedBean_SimpleModel 
{ 
    public function dispense() 
    { 
    $this->salt = cypher::getIV(32); 
    $this->tempPassword = cypher::getIV(8); 
    $this->password = md5($this->salt . $this->password); 
    } 

    public function update() 
    { 
    unset($this->tempPassword); 
    } 
} 

dispense()的生成密碼工作正常。 update()應該是在bean被保存之前運行的,所以它是(如果我將它的屬性保存爲null),但是,tempPassword仍然保存,即使我沒有設置它(該列也被創建即使我將它存儲爲空)。

基本上,這個問題歸結爲:如何擺脫tempPassword屬性,以便它不保存到數據庫?

回答

2

事實證明,其他人剛剛問了幾天前的確切問題,在readBean forum

基本上,redbean不會存儲任何類擴展的私有屬性。

然後將溶液很簡單:

class Model_employee extends RedBean_SimpleModel 
{ 
    private $tempPassword; 
    public function dispense() 
    { 
    $this->salt = cypher::getIV(32); 
    $this->tempPassword = cypher::getIV(8); 
    $this->password = md5($this->salt . $this->password); 
    } 
} 

這不會在數據庫中的密碼存儲,也創造列。 當然,如果我想讀取密碼,我必須添加一個getter(),但上面的解決了眼前的問題:)