Save entities in Database using One-To-Many relati

2019-08-24 04:31发布

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.

1条回答
Viruses.
2楼-- · 2019-08-24 05:01

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

查看更多
登录 后发表回答