2011-07-02 67 views
2

我使用Symfony 1.4,Doctrine 1.2和sfDoctrineGuardPlugin。在我的actions.class.php有:如何正確獲取ID?

$this->idsfguard = $this->getUser()->getGuardUser()->getId(); 

,如果我在這工作好我登錄,但如果然後我退出,我有錯誤:

Fatal error: Call to a member function getId() on a non-object in

我想:

if ($this->getUser()->getGuardUser()->isAuthenticated()){ 
    $this->idsfguard = $this->getUser()->getGuardUser()->getId(); 
    } 

但我有錯誤:

Fatal error: Call to a member function isAuthenticated() on a non-object in

回答

3
if ($this->getUser()->isAuthenticated()) { 
    $id = $this->getUser()->getGuardUser()->getId(); 
} 

isAuthenticated()方法是爲sfUser類,而不是sfGuardUser。如果用戶通過身份驗證,則只能通過sfUser訪問sfGuardUser類。

+0

謝謝!現在在模板成功我應該使用isset($ id)相同在清除php?在symfony做不同? –

+0

@Beded Black:isset()很好。只要確保將變量作爲「$ this-> id」傳遞給模板,然後可以執行isset($ id)。 – Tom