2012-08-11 65 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); 

纠正我,如果我错了。