我有一个包含表users
的数据库,其中包含字段:user_id, username, password
,....我还有players
表player_name, player_position
,...和FK user_id
。
图像关系:
我从数据库中创建了实体,但现在我想从这个用户那里得到像这样的$ user-> 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;
}
但是当我尝试$ user-> getPlayer()时,我总是得到null。 (并且user_id在玩家表中)
更新
我的玩家实体:
<?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;
但它仍然不起作用..
答案 0 :(得分:1)
您的注释未正确设置(特别是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实体。首先修复这些错误,它应该解决问题。
<强>更新强>
这是一种非常不同的设置,您希望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 Docs,这是一个非常简单的问题,在Symfony docs
中已经非常清楚地说明了