吸气剂实体类+ Symfony2中的Setter

时间:2014-01-20 16:50:40

标签: php mysql symfony doctrine-orm doctrine

我有一个包含表users的数据库,其中包含字段:user_id, username, password,....我还有playersplayer_name, player_position,...和FK user_id

我使用以下命令从我的数据库生成我的实体:

php app/console doctrine:mapping:import --force VolleyScoutBundle xml
php app/console doctrine:mapping:convert annotation ./src
php app/console doctrine:generate:entities VolleyScoutBundle

(没有产生任何错误......)

但在我的用户实体中,我没有属性playergetPlayer()setPlayer()。学说不会产生这个。所以现在我想自己创建它,这样我就可以获得user_id = user_id的播放器。

我试过这样的话:

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。当用户在玩家表中时也是如此。到目前为止,我的完整用户实体是:

<?php

namespace VolleyScout\VolleyScoutBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;

/**
 * Users
 *
 * @ORM\Table(name="users", indexes={@ORM\Index(name="fk_users_roles1_idx", columns={"role_id"})})
 * @ORM\Entity
 */
class Users implements AdvancedUserInterface
{
    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=45, nullable=false)
     */
    private $username;

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

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

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

    /**
     * @var string
     *
     * @ORM\Column(name="user_surname", type="string", length=255, nullable=false)
     */
    private $userSurname;

    /**
     * @var string
     *
     * @ORM\Column(name="user_email", type="string", length=255, nullable=false)
     */
    private $userEmail;

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

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

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

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

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

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

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

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

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

    /**
     * @var \VolleyScout\VolleyScoutBundle\Entity\Roles
     *
     * @ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Roles")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="role_id", referencedColumnName="role_id")
     * })
     */
    protected $role;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Teams", inversedBy="user")
     * @ORM\JoinTable(name="user_follows_teams",
     *   joinColumns={
     *     @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="team_id", referencedColumnName="team_id")
     *   }
     * )
     */
    private $team;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Competitions", inversedBy="user")
     * @ORM\JoinTable(name="user_follows_competitions",
     *   joinColumns={
     *     @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="competition_id", referencedColumnName="competition_id")
     *   }
     * )
     */
    private $competition;

    private $plainPassword;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->team = new \Doctrine\Common\Collections\ArrayCollection();
        $this->competition = new \Doctrine\Common\Collections\ArrayCollection();
        $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
    }


    /**
     * Set username
     *
     * @param string $username
     * @return Users
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

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

    /**
     * Set password
     *
     * @param string $password
     * @return Users
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

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

    /**
     * Set salt
     *
     * @param string $salt
     * @return Users
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;

        return $this;
    }

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

    /**
     * Set userFirstname
     *
     * @param string $userFirstname
     * @return Users
     */
    public function setUserFirstname($userFirstname)
    {
        $this->userFirstname = $userFirstname;

        return $this;
    }

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

    /**
     * Set userSurname
     *
     * @param string $userSurname
     * @return Users
     */
    public function setUserSurname($userSurname)
    {
        $this->userSurname = $userSurname;

        return $this;
    }

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

    /**
     * Set userEmail
     *
     * @param string $userEmail
     * @return Users
     */
    public function setUserEmail($userEmail)
    {
        $this->userEmail = $userEmail;

        return $this;
    }

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

    /**
     * Set userType
     *
     * @param string $userType
     * @return Users
     */
    public function setUserType($userType)
    {
        $this->userType = $userType;

        return $this;
    }

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

    /**
     * Set userToken
     *
     * @param string $userToken
     * @return Users
     */
    public function setUserToken($userToken)
    {
        $this->userToken = $userToken;

        return $this;
    }

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

    /**
     * Set userCreated
     *
     * @param \DateTime $userCreated
     * @return Users
     */
    public function setUserCreated($userCreated)
    {
        $this->userCreated = $userCreated;

        return $this;
    }

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

    /**
     * Set userModified
     *
     * @param \DateTime $userModified
     * @return Users
     */
    public function setUserModified($userModified)
    {
        $this->userModified = $userModified;

        return $this;
    }

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

    /**
     * Set userDeleted
     *
     * @param \DateTime $userDeleted
     * @return Users
     */
    public function setUserDeleted($userDeleted)
    {
        $this->userDeleted = $userDeleted;

        return $this;
    }

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

    /**
     * Set userLastlogin
     *
     * @param \DateTime $userLastlogin
     * @return Users
     */
    public function setUserLastlogin($userLastlogin)
    {
        $this->userLastlogin = $userLastlogin;

        return $this;
    }

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

    /**
     * Set userConfirmed
     *
     * @param \DateTime $userConfirmed
     * @return Users
     */
    public function setUserConfirmed($userConfirmed)
    {
        $this->userConfirmed = $userConfirmed;

        return $this;
    }

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

    /**
     * Set userLocked
     *
     * @param \DateTime $userLocked
     * @return Users
     */
    public function setUserLocked($userLocked)
    {
        $this->userLocked = $userLocked;

        return $this;
    }

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

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

    /**
     * Set role
     *
     * @param \VolleyScout\VolleyScoutBundle\Entity\Roles $role
     * @return Users
     */
    public function setRoles(\VolleyScout\VolleyScoutBundle\Entity\Roles $role = null)
    {
        $this->role = $role;

        return $this;
    }

    /**
     * Get role
     *
     * @return \VolleyScout\VolleyScoutBundle\Entity\Roles
     */
    public function getRoles()
    {
        return array($this->role->getRoleName());
    }

    /**
     * Add team
     *
     * @param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
     * @return Users
     */
    public function addTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team)
    {
        $this->team[] = $team;

        return $this;
    }

    /**
     * Remove team
     *
     * @param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
     */
    public function removeTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team)
    {
        $this->team->removeElement($team);
    }

    /**
     * Get team
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTeam()
    {
        return $this->team;
    }

    /**
     * Add competition
     *
     * @param \VolleyScout\VolleyScoutBundle\Entity\Competitions $competition
     * @return Users
     */
    public function addCompetition(\VolleyScout\VolleyScoutBundle\Entity\Competitions $competition)
    {
        $this->competition[] = $competition;

        return $this;
    }

    /**
     * Remove competition
     *
     * @param \VolleyScout\VolleyScoutBundle\Entity\Competitions $competition
     */
    public function removeCompetition(\VolleyScout\VolleyScoutBundle\Entity\Competitions $competition)
    {
        $this->competition->removeElement($competition);
    }

    /**
     * Get competition
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getCompetition()
    {
        return $this->competition;
    }

    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;
    }

    public function eraseCredentials()
    {
        $this->setPlainPassword(null);
    }

    public function getPlainPassword()
    {
        return $this->plainPassword;
    }

    public function setPlainPassword($plainPassword)
    {
        $this->plainPassword = $plainPassword;
    }


    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isAccountNonExpired()
    {
        return true;
    }

    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isAccountNonLocked()
    {
        return true;
    }

    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isCredentialsNonExpired()
    {
        return true;
    }

    /**
     * Implementation of AdvancedUserInterface method
     *
     * @return boolean
     */
    public function isEnabled()
    {
        // CHECK IF $this->confirmed is not null
        if($this->userConfirmed != null){
            return true;
        }
    }
}

我的玩家实体:

    <?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;
    }
}

更新
我尝试添加这个:

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

但它没有做任何事情。为了澄清,我的表关系: enter image description here

更新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 个答案:

答案 0 :(得分:3)

根据我读过的评论,您需要UsersPlayers之间的一对一关联。您可以将其设置为单向,如下所示:

class Players
{
    /**
     * @ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Users")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     */
    private $user;

    // ...
}

这意味着您可以从播放器($player->getUser())访问用户。但由于它是单向的,因此不会访问用户($user->getPlayer())的播放器。当$player中创建Users属性时,该原则是正确的。

您可以反过来定义关联:

class Users
{
    /**
     * @ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players")
     * @ORM\JoinColumn(name="player_id", referencedColumnName="player_id")
     */
    private $player;

    // ...
}

您现在可以$user->getPlayer(),但无法执行$player->getUser()

如果你想要两者,你必须将其设置为双向关联:

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

    // ...
}

class Users
{
    /**
     * @ORM\OneToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Players", mappedBy="user")
     */
    private $player;

    // ...
}

PS:我强烈建议您以单一形式(UserPlayer)命名您的课程,而不是复数形式(UsersPlayers),因为一个实例这些类的(对象)将代表1个用户或玩家(不是很多用户或玩家)。