2013-11-27 141 views
0

我收到一個錯誤'Error:調用未定義的方法Login \ LogBundle \ Entity \ User :: getToken()' 當試圖從另一個實體apiuserdetails。 user_id是apiuserdetails中的外鍵。我一直在關注示例http://symfony.com/doc/current/book/doctrine.html關係映射模型。我能夠使用getRepository(「LoginLogBundle:UserApiProfile」)從用戶實體獲取信息,但是如果按照他們描述的方式進行反向操作,則會出現錯誤。我在做映射時遇到了問題,但看不到它。會感謝一些幫助,謝謝。Symfony 2學說一對多關係映射難倒

控制器代碼

​​

}

實體 - 用戶

 namespace Login\LogBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity(repositoryClass="Login\LogBundle\Entity\Repository\UserRepository") 
* @ORM\Table(name="user") 
* @ORM\HasLifecycleCallbacks() 
* 
* User 
*/ 
class User{ 
    /** 
    * @var string 
    */ 
    private $password; 

    /** 
    * @var string 
    */ 
    private $name; 

    /** 
    * @var string 
    */ 
    private $username; 

    /** 
    * @var integer 
    */ 
    private $id; 

    /** 
    * @ORM\OneToMany(targetEntity="UserApiDetails", mappedBy="User") 
    */ 
    protected $userapidetails; 

    public function __construct() 
    { 
     $this->userapidetails = new ArrayCollection(); 
    } 

    /** 
    * Set password 
    * 
    * @param string $password 
    * @return User 
    */ 
    public function setPassword($password) 
    { 
     $this->password = $password; 

     return $this; 
    } 

    /** 
    * Get password 
    * 
    * @return string 
    */ 
    public function getPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * @return User 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 

    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set username 
    * 
    * @param string $username 
    * @return User 
    */ 

    public function setUsername($username) 
    { 
     $this->username = $username; 

     return $this; 
    } 

    /** 
    * Get username 
    * 
    * @return string 
    */ 

    public function getUsername() 
    { 
     return $this->username; 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 

    public function getId() 
    { 
     return $this->id; 
    } 
} 

實體 - ApiUserDetails

 namespace Login\LogBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity(repositoryClass="Login\LogBundle\Entity\Repository \UserApiDetailsRepository") 
* @ORM\Table(name="user_api_details") 
* @ORM\HasLifecycleCallbacks() 
* 
* UserApiDetails 
*/ 
class UserApiDetails{ 
    /** 
    * @var string 
    */ 
    private $apiScheme; 

    /** 
    * @var string 
    */ 
    private $apiUrl; 

    /** 
    * @var string 
    */ 
    private $token; 

    /** 
    * @var string 
    */ 
    private $tokenSecret; 

    /** 
    * @var integer 
    */ 
    private $apiId; 

    /** 
    * @var \Login\LogBundle\Entity\User 
    * 
    * @ORM\ManyToOne(targetEntity="User",inversedBy="userapidetails") 
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id") 
    */ 
    protected $user; 

    /** 
    * Set apiScheme 
    * 
    * @param string $apiScheme 
    * @return UserApiDetails 
    */ 
    public function setApiScheme($apiScheme) 
    { 
     $this->apiScheme = $apiScheme; 

     return $this; 
    } 

    /** 
    * Get apiScheme 
    * 
    * @return string 
    */ 
    public function getApiScheme() 
    { 
     return $this->apiScheme; 
    } 

    /** 
    * Set apiUrl 
    * 
    * @param string $apiUrl 
    * @return UserApiDetails 
    */ 
    public function setApiUrl($apiUrl) 
    { 
     $this->apiUrl = $apiUrl; 

     return $this; 
    } 

    /** 
    * Get apiUrl 
    * 
    * @return string 
    */ 
    public function getApiUrl() 
    { 
     return $this->apiUrl; 
    } 

    /** 
    * Set token 
    * 
    * @param string $token 
    * @return UserApiDetails 
    */ 
    public function setToken($token) 
    { 
     $this->token = $token; 

     return $this; 
    } 

    /** 
    * Get token 
    * 
    * @return string 
    */ 
    public function getToken() 
    { 
     return $this->token; 
    } 

    /** 
    * Set tokenSecret 
    * 
    * @param string $tokenSecret 
    * @return UserApiDetails 
    */ 
    public function setTokenSecret($tokenSecret) 
    { 
     $this->tokenSecret = $tokenSecret; 

     return $this; 
    } 

    /** 
    * Get tokenSecret 
    * 
    * @return string 
    */ 
    public function getTokenSecret() 
    { 
     return $this->tokenSecret; 
    } 

    /** 
    * Get apiId 
    * 
    * @return integer 
    */ 
    public function getApiId() 
    { 
     return $this->apiId; 
    } 

    /** 
    * Set user 
    * 
    * @param \Login\LogBundle\Entity\User $user 
    * @return UserApiDetails 
    */ 
    public function setUser(\Login\LogBundle\Entity\User $user = null) 
    { 
     $this->user = $user; 

     return $this; 
    } 

    /** 
    * Get user 
    * 
    * @return \Login\LogBundle\Entity\User 
    */ 
    public function getUser() 
    { 
     return $this->user; 
    } 
} 

回答

0

如果你得到\Login\LogBundle\Entity\User你沒有setToken並在錯誤信息中說明。因爲你沒有導航屬性到\Login\LogBundle\Entity\UserApiDetails,事實上,如果你朝着這個方向前進,你會陷入困境。您可以從User獲取導航屬性,以獲取(單個或列表)UserApiDetails,或者獲取UserApiDetails並使用導航屬性獲取User信息。在後一種情況下,你可以在樹枝簡單地使用{{ userapi.user.token }}

+0

我想你提到的導航屬性像下面做,或者你可以給我正確的代碼,請/ ** * @ORM \一對多(targetEntity =「UserApiDetails」 ,mappedBy =「User」) */ protected $ userapidetails; public function __construct() {this-> userapidetails = new ArrayCollection(); } @saamorim – GAV

+0

如果必須顯式實現訪問器,才能使用像getUserapidetails()和setUserapidetails(UserApiDetails $ userapidetails)這樣的使用方式,才能使用api中的導航。無論如何,請記住,您正在獲得一系列userapidetails,因此請注意,因爲{{user.userapidetails}}將返回一個數組。 – saamorim

+0

感謝我們慢慢到達那裏,我不知道如何渲染這個數組在枝中我在控制器中執行此操作返回$ this-> render('LoginLogBundle:Default:userprofile.html.twig',array('細節'=> $ user-> getUserapidetails()));然後在樹枝{%for find in details%} {{find.token}} {%endfor%}沒有任何東西@saamorim – GAV