2011-07-02 65 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