Symfony2 Doctrine设置ManyToOne关系中字段的名称

时间:2015-11-14 16:41:11

标签: symfony doctrine relationship

我正试图将实体与彼此联系起来

当我运行doctrine:schema:update --dump-sql

我得到以下内容:

ALTER TABLE fd_dailyFantasyplayer ADD playerID_id INT DEFAULT NULL;
ALTER TABLE fd_dailyFantasyplayer ADD CONSTRAINT FK_7DB7E9EBE1883E45 FOREIGN KEY (playerID_id) REFERENCES fd_player (id);
CREATE INDEX IDX_7DB7E9EBE1883E45 ON fd_dailyFantasyplayer (playerID_id);

注意名称ADD playerID_id

我更喜欢将该列称为playerID

我的实体:

但是,如果我添加列注释:* @ORM\Column(name="playerID")

ALTER TABLE fd_dailyFantasyplayer ADD playerID INT NOT NULL;

省略索引和约束。

如何将列名设置为playerID(什么是教条将额外的_id添加到列名称的原因?

播放器:     

namespace FantasyPro\DataBundle\Entity;

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

/**
 * Player
 *
 * @ORM\Table(name="fd_player")
 * @ORM\Entity(repositoryClass="FantasyPro\DataBundle\Entity\PlayerRepository")
 */
class Player
    {

    public function __construct()
    {
        $this->contestEntries = new ArrayCollection();
        $this->dailyFantasyPlayers = new ArrayCollection();
    }
    /**
     * @var integer
     *
     * @ORM\Column(type="integer", length=32, name="id")
     * 
     * @ORM\Id
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=10, nullable=true, name="team")
     */
    private $team;

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

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=50, nullable=true, name="firstName")
     */
    private $firstName;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=50, nullable=true, name="lastName")
     */
    private $lastName;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=10, nullable=true, name="position")
     */
    private $position;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=50, nullable=true, name="status")
     */
    private $status;

    /**
     *   @ORM\ManyToMany(targetEntity="FantasyPro\GameBundle\Entity\ContestEntry", mappedBy="players")
     */
    private $contestEntries;

    /**
     *   @ORM\OneToMany(targetEntity="FantasyPro\DataBundle\Entity\DailyFantasyPlayer", mappedBy="playerID")
     */
    private $dailyFantasyPlayers;

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

    /**
     * Set team
     *
     * @param string $team
     *
     * @return Player
     */
    public function setTeam( $team )
    {
        $this->team = $team;

        return $this;
    }

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

    /**
     * Set number
     *
     * @param integer $number
     *
     * @return Player
     */
    public function setNumber( $number )
    {
        $this->number = $number;

        return $this;
    }

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

    /**
     * Set firstName
     *
     * @param string $firstName
     *
     * @return Player
     */
    public function setFirstName( $firstName )
    {
        $this->firstName = $firstName;

        return $this;
    }

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

    /**
     * Set lastName
     *
     * @param string $lastName
     *
     * @return Player
     */
    public function setLastName( $lastName )
    {
        $this->lastName = $lastName;

        return $this;
    }

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

    /**
     * Set position
     *
     * @param string $position
     *
     * @return Player
     */
    public function setPosition( $position )
    {
        $this->position = $position;

        return $this;
    }

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

    /**
     * Set status
     *
     * @param string $status
     *
     * @return Player
     */
    public function setStatus( $status )
    {
        $this->status = $status;

        return $this;
    }

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

    /**
     * @param null $ContestEntries
     *
     */
    public function setContestEntries( $ContestEntries = null)
    {
        $this->contestEntries = $ContestEntries;
    }

    /**
     * @return mixed
     */
    public function getDailyFantasyPlayers()
    {
        return $this->dailyFantasyPlayers;
    }

    /**
     * @param DailyFantasyPlayer $dailyFantasyPlayers
     *
     */
    public function setDailyFantasyPlayers( DailyFantasyPlayer $dailyFantasyPlayers = null)
    {
        $this->dailyFantasyPlayers = $dailyFantasyPlayers;
    }

    /**
     * @param DailyFantasyPlayer $dailyFantasyPlayers
     *
     * @return ArrayCollection
     *
     */
    public function addDailyFantasyPlayers(DailyFantasyPlayer $dailyFantasyPlayers = null)
    {
        if(! $this->dailyFantasyPlayers->contains($dailyFantasyPlayers)){
            $this->dailyFantasyPlayers->add($dailyFantasyPlayers);
        }

        return $this->dailyFantasyPlayers;
    }

    /**
     * @param DailyFantasyPlayer $dailyFantasyPlayers
     *
     * @return ArrayCollection
     *
     */
    public function removeDailyFantasyPlayers(DailyFantasyPlayer $dailyFantasyPlayers)
    {
        if($this->dailyFantasyPlayers->contains($dailyFantasyPlayers)){
            $this->dailyFantasyPlayers->removeElement($dailyFantasyPlayers);
        }

        return $this->dailyFantasyPlayers;
    }
}

DailyFantasyPlayer:

<?php

namespace FantasyPro\DataBundle\Entity;


use Doctrine\ORM\Mapping as ORM;

/**
 * DailyFantasyPlayer
 *
 * @ORM\Table(name="fd_dailyFantasyplayer")
 * @ORM\Entity(repositoryClass="FantasyPro\DataBundle\Entity\DailyFantasyPlayerRepository")
 */
class DailyFantasyPlayer
{

    /**
     * @var integer
     * @ORM\Id
     * @ORM\Column(type="integer", name="id")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="FantasyPro\DataBundle\Entity\Player", inversedBy="dailyFantasyPlayers")
     * @ORM\Column(name="playerID")
     */
    private $playerID;

    /**
     * @var \DateTime
     *
     * @ORM\Column(type="datetime", nullable=true, name="date")
     */
    private $date;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=50, nullable=true, name="shortName")
     */
    private $shortName;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=50, nullable=true, name="name")
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=10, nullable=true, name="team")
     */
    private $team;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=10, nullable=true, name="opponent")
     */
    private $opponent;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=10, nullable=true, name="position")
     */
    private $position;

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

    /**
     * @var string
     *
     * @ORM\Column(type="decimal", nullable=true, name="lastGameFantasyPoints")
     */
    private $lastGameFantasyPoints;

    /**
     * @var string
     *
     * @ORM\Column(type="decimal", nullable=true, name="projectedFantasyPoints")
     */
    private $projectedFantasyPoints;

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

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

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=50, nullable=true, name="status")
     */
    private $status;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=50, nullable=true, name="statusCode")
     */
    private $statusCode;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=50, nullable=true, name="statusColor")
     */
    private $statusColor;

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

    /**
     * Set date
     *
     * @param \DateTime $date
     *
     * @return DailyFantasyPlayer
     */
    public function setDate( $date )
    {
        $this->date = $date;

        return $this;
    }

    /**
     * Get date
     *
     * @return \DateTime
     */
    public function getDate()
    {
        return $this->date;
    }

    /**
     * Set shortName
     *
     * @param string $shortName
     *
     * @return DailyFantasyPlayer
     */
    public function setShortName( $shortName )
    {
        $this->shortName = $shortName;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set team
     *
     * @param string $team
     *
     * @return DailyFantasyPlayer
     */
    public function setTeam( $team )
    {
        $this->team = $team;

        return $this;
    }

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

    /**
     * Set opponent
     *
     * @param string $opponent
     *
     * @return DailyFantasyPlayer
     */
    public function setOpponent( $opponent )
    {
        $this->opponent = $opponent;

        return $this;
    }

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

    /**
     * Set position
     *
     * @param string $position
     *
     * @return DailyFantasyPlayer
     */
    public function setPosition( $position )
    {
        $this->position = $position;

        return $this;
    }

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

    /**
     * Set salary
     *
     * @param integer $salary
     *
     * @return DailyFantasyPlayer
     */
    public function setSalary( $salary )
    {
        $this->salary = $salary;

        return $this;
    }

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

    /**
     * Set lastGameFantasyPoints
     *
     * @param string $lastGameFantasyPoints
     *
     * @return DailyFantasyPlayer
     */
    public function setLastGameFantasyPoints( $lastGameFantasyPoints )
    {
        $this->lastGameFantasyPoints = $lastGameFantasyPoints;

        return $this;
    }

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

    /**
     * Set projectedFantasyPoints
     *
     * @param string $projectedFantasyPoints
     *
     * @return DailyFantasyPlayer
     */
    public function setProjectedFantasyPoints( $projectedFantasyPoints )
    {
        $this->projectedFantasyPoints = $projectedFantasyPoints;

        return $this;
    }

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

    /**
     * Set opponentRank
     *
     * @param integer $opponentRank
     *
     * @return DailyFantasyPlayer
     */
    public function setOpponentRank( $opponentRank )
    {
        $this->opponentRank = $opponentRank;

        return $this;
    }

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

    /**
     * @return int
     */
    public function getOpponentPositionRank()
    {
        return $this->opponentPositionRank;
    }

    /**
     * @param int $opponentPositionRank
     */
    public function setOpponentPositionRank( $opponentPositionRank )
    {
        $this->opponentPositionRank = $opponentPositionRank;
    }

    /**
     * Set status
     *
     * @param string $status
     *
     * @return DailyFantasyPlayer
     */
    public function setStatus( $status )
    {
        $this->status = $status;

        return $this;
    }

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

    /**
     * Set statusCode
     *
     * @param string $statusCode
     *
     * @return DailyFantasyPlayer
     */
    public function setStatusCode( $statusCode )
    {
        $this->statusCode = $statusCode;

        return $this;
    }

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

    /**
     * Set statusColor
     *
     * @param string $statusColor
     *
     * @return DailyFantasyPlayer
     */
    public function setStatusColor( $statusColor )
    {
        $this->statusColor = $statusColor;

        return $this;
    }

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

    /**
     * @param null $playerID
     *
     * @return $this
     */
    public function setPlayerID( $playerID )
    {
        $this->playerID = $playerID;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getPlayerID()
    {
        return $this->playerID;
    }
}

1 个答案:

答案 0 :(得分:1)

您应该调用实体player而不是playerID的属性,因为它代表Player个对象。如果您要将表格列名更改为playerID而不是默认player_id,请使用JoinColumn注释:

/**
 * @ORM\ManyToOne(targetEntity="Player", inversedBy="dailyFantasyPlayers")
 * @ORM\JoinColumn(name="playerID")
 */
 private $player;

或者从Doctrine 2.3开始,您可以实现自己的naming strategy以使其自动生成。