2014-01-20 120 views
1

我有一個表users與數據庫的字段:user_id, username, password,...。我也有一個表playersplayer_name, player_position,...和一個FK user_id。 圖片關係: enter image description here映射一對一的關係+ Symfony2

我genered從數據庫中的實體,但現在我想從用戶得到的球員像這樣用戶$> getPlayer()。而學說沒有在用戶實體中生成玩家,只在玩家實體中生成用戶。

這是我現在有:

/** 
* @var \VolleyScout\VolleyScoutBundle\Entity\Players 
* 
* @ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players") 
* @JoinColumn(name="user_id", referencedColumnName="userId") 
*/ 
private $player; 

/** 
* Get player 
* 
* @return \VolleyScout\VolleyScoutBundle\Entity\Players 
*/ 

public function getPlayer() { 
    return $this->player; 
} 
/** 
* Set player 
* 
* @param \VolleyScout\VolleyScoutBundle\Entity\Players $player 
* @return Users 
*/ 
public function setPlayer(\VolleyScout\VolleyScoutBundle\Entity\Players $player = null){ 
    $this->player = $player; 

    return $this; 
} 

但是當我嘗試$用戶> getPlayer()我總是空。 (和user_id是在球員表)

UPDATE:
我的球員實體:

<?php 

namespace VolleyScout\VolleyScoutBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Players 
* 
* @ORM\Table(name="players", indexes={@ORM\Index(name="fk_players_users1_idx", columns={"user_id"}), @ORM\Index(name="fk_players_teams1_idx", columns={"team_id"}), @ORM\Index(name="fk_players_myteam1_idx", columns={"myteam_id"})}) 
* @ORM\Entity 
*/ 
class Players 
{ 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="player_name", type="string", length=255, nullable=false) 
    */ 
    private $playerName; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="player_licensenumber", type="string", length=45, nullable=false) 
    */ 
    private $playerLicensenumber; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="player_position", type="string", nullable=false) 
    */ 
    private $playerPosition; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="player_birthyear", type="integer", nullable=true) 
    */ 
    private $playerBirthyear; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="player_id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $playerId; 

    /** 
    * @var \VolleyScout\VolleyScoutBundle\Entity\Myteam 
    * 
    * @ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Myteam") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="myteam_id", referencedColumnName="myteam_id") 
    * }) 
    */ 
    private $myteam; 

    /** 
    * @var \VolleyScout\VolleyScoutBundle\Entity\Teams 
    * 
    * @ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Teams") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="team_id", referencedColumnName="team_id") 
    * }) 
    */ 
    private $team; 

    /** 
    * @var \VolleyScout\VolleyScoutBundle\Entity\Users 
    * 
    * @ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Users") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id") 
    * }) 
    */ 
    private $user; 



    /** 
    * Set playerName 
    * 
    * @param string $playerName 
    * @return Players 
    */ 
    public function setPlayerName($playerName) 
    { 
     $this->playerName = $playerName; 

     return $this; 
    } 

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

    /** 
    * Set playerLicensenumber 
    * 
    * @param string $playerLicensenumber 
    * @return Players 
    */ 
    public function setPlayerLicensenumber($playerLicensenumber) 
    { 
     $this->playerLicensenumber = $playerLicensenumber; 

     return $this; 
    } 

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

    /** 
    * Set playerPosition 
    * 
    * @param string $playerPosition 
    * @return Players 
    */ 
    public function setPlayerPosition($playerPosition) 
    { 
     $this->playerPosition = $playerPosition; 

     return $this; 
    } 

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

    /** 
    * Set playerBirthyear 
    * 
    * @param integer $playerBirthyear 
    * @return Players 
    */ 
    public function setPlayerBirthyear($playerBirthyear) 
    { 
     $this->playerBirthyear = $playerBirthyear; 

     return $this; 
    } 

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

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

    /** 
    * Set myteam 
    * 
    * @param \VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam 
    * @return Players 
    */ 
    public function setMyteam(\VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam = null) 
    { 
     $this->myteam = $myteam; 

     return $this; 
    } 

    /** 
    * Get myteam 
    * 
    * @return \VolleyScout\VolleyScoutBundle\Entity\Myteam 
    */ 
    public function getMyteam() 
    { 
     return $this->myteam; 
    } 

    /** 
    * Set team 
    * 
    * @param \VolleyScout\VolleyScoutBundle\Entity\Teams $team 
    * @return Players 
    */ 
    public function setTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team = null) 
    { 
     $this->team = $team; 

     return $this; 
    } 

    /** 
    * Get team 
    * 
    * @return \VolleyScout\VolleyScoutBundle\Entity\Teams 
    */ 
    public function getTeam() 
    { 
     return $this->team; 
    } 

    /** 
    * Set user 
    * 
    * @param \VolleyScout\VolleyScoutBundle\Entity\Users $user 
    * @return Players 
    */ 
    public function setUser(\VolleyScout\VolleyScoutBundle\Entity\Users $user = null) 
    { 
     $this->user = $user; 

     return $this; 
    } 

    /** 
    * Get user 
    * 
    * @return \VolleyScout\VolleyScoutBundle\Entity\Users 
    */ 
    public function getUser() 
    { 
     return $this->user; 
    } 
} 

更新2:在我的用戶實體類我現在有:

/** 
* @var \VolleyScout\VolleyScoutBundle\Entity\Players 
* 
* @ORM\OneToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players") 
* @ORM\JoinColumns({ 
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id") 
* }) 
*/ 
private $player; 

但它仍然不起作用..

回答

1

Your註釋設置不正確(特別是JoinColumn)。與此

/** 
* @var \VolleyScout\VolleyScoutBundle\Entity\Players 
* 
* @ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players") 
* @JoinColumn(name="user_id", referencedColumnName="userId") 
*/ 
private $player; 

:有趣的是,你沒有得到一個錯誤,但反正...替換此

/** 
* @var \VolleyScout\VolleyScoutBundle\Entity\Players 
* 
* @ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players") 
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id") 
*/ 
private $player; 

通知您referencedColumnName應該是因爲它是在顯示的名稱數據庫,而不是Doxtrine實體。從修復這些錯誤開始,它應該解決問題。

UPDATE

這是一個非常不同的設置你想要一個manyToOne關係。

/** 
* @var \VolleyScout\VolleyScoutBundle\Entity\Players 
* 
* @ORM\OneToMany(targetEntity="Players", mappedBy="user_id") 
*/ 
private $players; 

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

因爲$players現在可以有多個值,應設置爲ArrayCollection。爲了便於閱讀,我還製作了變量$players而不是$player

你真的應該熟悉Symfony的文檔,這是在Symfony docs

+0

它不工作描述很清楚一個很簡單的問題。但是我的自動生成的玩家實體顯示了與用戶表的多對一關係。我用Players實體更新了我的開始帖子。我現在應該在我的用戶表中作爲註釋發佈什麼內容? – nielsv

+0

這取決於,你想manytoOne或onetoOne? – Sehael

+0

我將我的開始帖子更新爲manytoOne,但它仍然無效。你可以看看嗎? – nielsv