2012-08-11 66 views
2
protected $_name = 'usertable'; //table name for database [Impt] Please change accordingly 
protected $_temp;  

function resetPass($userid) 
{ 
    $pass = new Application_Model_Register(); 
    $salt = $pass->generateSalt(); 
    $temp = $pass->generatePass(); 
    $this->_temp = (string) $temp; 


    $data = array(
      'password' => hash("sha256", ($salt. $temp)), 
      'salt' => $salt, //get salt from generateSalt() 
    ); 

    //$auth= Zend_Auth::getInstance(); //declare zend_auth to get instance 
    //$user= $auth->getIdentity(); //get identity of user 
    //$userid = $user->userid; //get userid of user 

    echo $temp; 


    $this->update($data, 'userid = '. (int)($userid)); 
    return $this; 
} 

function getTemp() 
{ 
    return parent::$this->_temp; 
} 

這是我在Model中的代碼。我試圖返回$ _temp,因此我做了$ this-> temp = $ temp。我的問題是,它返回NULL。變量一直返回NULL,php

public function sendEmail($email) 
{ 

    $email = $_POST['email']; 

    $userid = '30'; 

    $reset = new Application_Model_DbTable_Register(); 
    $reset->resetPass($userid); 

    $pswd = new Application_Model_DbTable_Register(); 
    $pswd = $pswd->getTemp(); 
    var_dump($pswd); 

    $mail = new Zend_Mail(); 

    $mail->setFrom('[email protected]', 'Inexorable Beauty'); 
    $mail->addTo($email, $email); 
    $mail->setSubject('Inexorable Beauty: Password Reset'); 
    $mail->setBodyText('Dear Customer, 

      You have requested to reset your password. 
      This is the temporary password: '.$pswd.' 

      Please log in immediately and change your password. 
      Thank You. 

      Yours Sincerely, 
      Inexorable Beauty'); 

    $mail->send(); 

    if($mail->send()) 
    { 
     echo "Email successfully sent!"; 
    } 
    else 
    { 
     echo "Email was not sent"; 
    } 



} 

這是我的控制器代碼。我正嘗試將臨時密碼發送給客戶的電子郵件。我從我的模型中調用getTemp()函數來獲取passwd字符串,但正如你所看到的,我做了var_dump($ pswd)並且它一直返回NULL。任何解決方案

+0

謝謝你們。我在$ _temp前添加了一個全局變量,它工作正常。似乎變量不能在函數之外使用。 http://php.net/manual/en/language.variables.scope.php – 2012-08-11 20:48:44

回答

1

您使用

return parent::$this->_temp; 

是否$_temp存在於父對象?它會出現在你定義的這個對象中,並且它不存在於你希望繼承的父對象中。

+0

刪除父母。我返回$ this - > _ temp,仍然給我NULL。 – 2012-08-11 18:15:50

1

它看起來像你正在創建兩個DbTable_Register對象,調用第一個對象,而不是第二個對象,然後你試圖從第二個對象獲取臨時密碼。第二個不會有溫度,因爲您沒有在第二個對象上調用resetPass

嘗試改變:

$reset = new Application_Model_DbTable_Register(); 
$reset->resetPass($userid); 

$pswd = new Application_Model_DbTable_Register(); 
$pswd = $pswd->getTemp(); 
var_dump($pswd); 

要:

$reset = new Application_Model_DbTable_Register(); 
$reset->resetPass($userid); 
$pswd = $reset->getTemp(); 

var_dump($pswd); 

看看是否能工程。

1

第一個:檢查Application_Model_Register :: generatePass();功能

:上resetPass功能刪除此:

$this->_temp = (string) $temp; 

,並添加:

if (empty($temp)){ 
    $this->_temp = (string) $temp; 
} 

是它返回的東西的價值?如果它沒有那麼generatePass()函數是錯誤

:您的控制器上

$reset = new Application_Model_DbTable_Register(); 
$reset->resetPass($userid); 
$pswd = new Application_Model_DbTable_Register(); 
$pswd = $pswd->getTemp(); 
var_dump($pswd); 

不要讓像$雙對象重置$ PSWD

嘗試:

$reset = new Application_Model_DbTable_Register(); 
$pswd = $reset->resetPass($userid)->getTemp(); 
echo "<pre>"; 
print_r($pswd); 

糾正我,如果我錯了。