使用Doctrine 2和Zend Framework 2使用一对多关系将实体保存在数据库中 [英] Save entities in Database using One-To-Many relationship using Doctrine 2 and Zend Framework 2

查看:185
本文介绍了使用Doctrine 2和Zend Framework 2使用一对多关系将实体保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个直截了当的问题,但是由于它与Doctrine 2和Zend Forms捆绑在一起,所以试着在这里寻求专家的支持。



让我从我的实体开始
小组实体:

  / ** 
*
* @ ORM\Entity
* @ ORM\Table(name =team)
* @property string $ teamName
* @property int $ teamId
* /
class Team
{
/ **
* @ ORM\Id
* @ ORM\Column(type =integer,name =teamId,unique = true);
* /
protected $ teamId;

/ **
* @ ORM\Column(type =string)
* /
protected $ teamName;

/ **
* @ ORM\OneToMany(targetEntity =TeamPlayers,mappedBy =team,cascade = {persist})
* /
protected $ player;

/ **
*魔术getter暴露受保护的属性。
*
* @param string $ property
* @return mixed
* /
public function __get($ property)
{
return $ this-> $ property;
}

/ **
*用于保存受保护属性的魔术设置器。
*
* @param string $ property
* @param mixed $ value
* /
public function __set($ property,$ value)
{
$ this-> $ property = $ value;
}
}

玩家实体:

  / ** 
*
* @ ORM\Entity
* @ ORM\Table(name =player )
* @property string $ playerName
* @property int $ playerId
* /
class Player
{
/ **
* @ ORM\Id
* @ ORM\Column(type =integer,name =playerId,unique = true);
* /
protected $ playerId;

/ **
* @ ORM\Column(type =string)
* /
protected $ playerName;

/ **
* @ ORM\OneToMany(targetEntity =TeamPlayers,mappedBy =player,cascade = {persist})
* /
protected $ team;

/ **
*魔术getter暴露受保护的属性。
*
* @param string $ property
* @return mixed
* /
public function __get($ property)
{
return $ this-> $ property;
}

/ **
*用于保存受保护属性的魔术设置器。
*
* @param string $ property
* @param mixed $ value
* /
public function __set($ property,$ value)
{
$ this-> $ property = $ value;
}
}

TeamPlayer实体:

  / ** 
*
* @ ORM\Entity
* @ ORM\Table(name =teamplayer )
* @property int $ teamId
* @property int $ playerId
* /
class TeamPlayer
{
/ **
* @ ORM\Id
* @Column(type =integer)
* /
protected $ playerId;

/ **
* @ ORM\Id
* @Column(type =integer)
* /
protected $ teamId;

/ **
* @ ORM\ManyToOne(targetEntity =Team,inversedBy =player,cascade = {persist})
* /
protected $ team;

/ **
* @ ORM\ManyToOne(targetEntity =Player,inversedBy =team,cascade = {persist})
* /
protected $ player;


/ **
*设置播放器
*
* @param \Entity\Player $ player
* /
public function setPlayer(\Entity\Player $ player)
{
$ this-> player = $ player;
}

/ **
*获取播放器
*
* @return \Entity\Player
* /
public function getPlayer()
{
return $ this-> player;
}

/ **
*设置小组
*
* @param \Entity\Team $ team
* /
public function setTeam(\Entity\Team $ team)
{
$ this-> team = $ team;
}

/ **
*获取团队
*
* @return \Entity\团队
* /
public function getTeam()
{
return $ this-> team;
}
}

?>

所以我现在最初有一个Zend Forms用于添加玩家,另一个用于添加团队,用户可以添加具有团队名称和团队ID的团队,并且最终他可以为特定团队选择玩家,即一个团队可以具有许多玩家。



我在addAction()中添加控制器中选定玩家的团队时遇到了问题,如何在团队中添加团队?我可以解决这些关系映射,我需要添加更多的方法来更新teamplayer表。如果有人解释如何在控制器中这样做,对于像我这样的人对Zend和Doctrine越来越有帮助。



EDIT

  *播放器表:* 

playerId - - Unique
playerName - varchar - Unique


*团队表:*

teamId - int - PK - Unique
teamName - varchar - Unique

* TeamPlayer表:*

playerId - int - PK,FK - 引用player.playerId
teamId - int - PK,FK - references team.teamId

我知道如何坚持对象的单个表或使用Doctrine中的连接获取数据,但我没有任何线索如何保存实体与这种表之间的关系。

解决方案

除了你的关联beeing错误,在你的模型设计TeamPlayer实体将是两个协会的拥有方。所以你必须这样做:

  $ teamPlayer = new TeamPlayer; 
$ teamPlayer-> setPlayer($ player);
$ teamPlayer-> setTeam($ team);

$ entityManager-> persist($ teamPlayer);
$ entityManager-> flush();







在播放器端关联的示例:

  / ** 
* @ManyToMany(targetEntity =Team)
* @JoinTable(name =Team_Player ,
* joinColumns = {@ JoinColumn(name =Player_id,referencedColumnName =id)},
* reverseJoinColumns = {@ JoinColumn(name =Team_id,referencedColumnName =id)}
*)
* /
protected $ teams;

还可以选中 doctrine文档适用于ManyToMany


I have got a straight forward problem, but since it got tied up with Doctrine 2 and Zend Forms, giving it a try here for support from the experts.

Let me start with my entities Team Entity:

/**
 *
 * @ORM\Entity
 * @ORM\Table(name="team")
 * @property string $teamName
 * @property int $teamId
 */
class Team
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="teamId", unique=true);
     */
    protected $teamId;

    /**
     * @ORM\Column(type="string")
     */
    protected $teamName;

    /**
     * @ORM\OneToMany(targetEntity="TeamPlayers", mappedBy="team", cascade={"persist"})
     */
    protected $player;

    /**
     * Magic getter to expose protected properties.
     *
     * @param string $property
     * @return mixed
     */
     public function __get($property)
    {
        return $this->$property;
    }

    /**
     * Magic setter to save protected properties.
     *
     * @param string $property
     * @param mixed $value
     */
    public function __set($property, $value)
    {
        $this->$property = $value;
    }
}

Player Entity:

/**
 *
 * @ORM\Entity
 * @ORM\Table(name="player")
 * @property string $playerName
 * @property int $playerId
 */
class Player
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="playerId", unique=true);
     */
    protected $playerId;

    /**
     * @ORM\Column(type="string")
     */
    protected $playerName;

    /**
     * @ORM\OneToMany(targetEntity="TeamPlayers", mappedBy="player", cascade={"persist"})
     */
    protected $team;

    /**
     * Magic getter to expose protected properties.
     *
     * @param string $property
     * @return mixed
     */
     public function __get($property)
    {
        return $this->$property;
    }

    /**
     * Magic setter to save protected properties.
     *
     * @param string $property
     * @param mixed $value
     */
    public function __set($property, $value)
    {
        $this->$property = $value;
    }
}

TeamPlayer Entity:

/**
 *
 * @ORM\Entity
 * @ORM\Table(name="teamplayer")
 * @property int $teamId
 * @property int $playerId
 */
class TeamPlayer 
{
    /**
     * @ORM\Id
     * @Column(type="integer")
     */
    protected $playerId;

    /**
     * @ORM\Id
     * @Column(type="integer")
     */
    protected $teamId;

    /**
     * @ORM\ManyToOne(targetEntity="Team", inversedBy="player", cascade={"persist"})
     */
    protected $team;

    /**
     * @ORM\ManyToOne(targetEntity="Player", inversedBy="team", cascade={"persist"})
     */
    protected $player;


    /**
     * Set player
     *
     * @param \Entity\Player $player
     */
    public function setPlayer(\Entity\Player $player)
    {
        $this->player = $player;
    }

    /**
     * Get player
     *
     * @return  \Entity\Player
     */
    public function getPlayer()
    {
        return  $this->player;
    }

    /**
     * Set team
     *
     * @param \Entity\Team $team     
     */
    public function setTeam(\Entity\Team $team)
    {
        $this->team = $team;
    }

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

?>

So I now I have the Zend Forms initially one for adding players and another one for adding teams, where the user can add a team with team name and team id and finally he can select players for the particular team, i.e one team can have many players. So the user can select multiple players in the form before adding the to the team.

My question when adding the team with selected players in my controller in addAction(), how can I work out with these relation mappings, do I need to add any more methods for updating the teamplayer table. In case if somebody explains how to do it in the controller, would be very helpful for people like me getting more and more into Zend and Doctrine.

EDIT

*Player Table:*

playerId    -- int      -- PK, AI -- Unique
playerName  -- varchar  -- Unique


*Team Table:*

teamId    -- int      -- PK -- Unique
teamName  -- varchar  -- Unique

*TeamPlayer Table:*

playerId  -- int  -- PK, FK -- references player.playerId
teamId    -- int  -- PK, FK -- references team.teamId

P.S. I have knowledge about how to persist objects for individual tables or to fetch the data using joins in Doctrine, but I have got no clue how to save the entities with this sort of relations between the tables. So do consider this as a learners question.

解决方案

apart from your associations beeing wrong, in your model design the TeamPlayer Entity would be the owning side of both associations. So you would have to do something like this:

$teamPlayer = new TeamPlayer;
$teamPlayer->setPlayer($player);
$teamPlayer->setTeam($team);

$entityManager->persist($teamPlayer);
$entityManager->flush(); 

BUT

have a look at the ManyToMany association which will make your associations and code more readable and less to write.

example for association on the player side:

/**
 * @ManyToMany(targetEntity="Team")
 * @JoinTable(name="Team_Player",
 *      joinColumns={@JoinColumn(name="Player_id", referencedColumnName="id")},
 *      inverseJoinColumns={@JoinColumn(name="Team_id", referencedColumnName="id")}
 *      )
 */
protected $teams;

also check the doctrine documentation for ManyToMany

这篇关于使用Doctrine 2和Zend Framework 2使用一对多关系将实体保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆