gpt4 book ai didi

php - 从其他表添加数据到表单+ Symfony2

转载 作者:行者123 更新时间:2023-11-29 13:19:29 26 4
gpt4 key购买 nike

我有一个表用户,其中包含不同的字段。我还有一个表玩家,其中包含 FK user_id(以及其他字段)。

现在我有一个注册表单来创建用户,但我还想为玩家添加一个字段。

这是我现在的表格:

$builder
->add('userFirstname', 'text', [
'label' => 'Given name',
'attr' => [
'placeholder' => 'Enter your given name.',
],
])
->add('userSurname', 'text', [
'label' => 'Family name',
'attr' => ['placeholder' => 'Enter your family name.'],
])
->add('userType', 'choice', array(
'choices' => array('E' => 'ENTHOUSIAST', 'P' => 'PLAYER', 'T' => 'TRAINER'),
'label' => 'User Type',
'attr' => array('placeholder' => 'User Type')
))
->add('player')
->add('userUsername', 'text', [
'label' => 'Username',
'attr' => ['placeholder' => 'Enter a username.'],
])
->add('userEmail', 'email', [
'label' => 'Email address',
'attr' => ['placeholder' => 'Enter your email address.'],
])
->add('userPassword', 'repeated', [
'type' => 'password',
'first_name' => 'password',
'first_options' => [
'label' => 'Password',
'attr' => ['placeholder' => 'Enter a password.'],
],
'second_name' => 'confirm',
'second_options' => [
'label' => 'Password (repeat)',
'attr' => ['placeholder' => 'Repeat the password.'],
],
'invalid_message' => 'The passwords are not identical.',
])
->add('btn_register', 'submit', [
'label' => 'Register',
])
;

在我的玩家表中,我有“player_birthyear”。我如何将其添加到我的表单中?我已经尝试将player.birthyear 添加到我的表单中,但没有成功。

更新:
我的用户实体类:

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

/**
* @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_password", type="string", length=60, nullable=false)
*/
private $userPassword;

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

/**
* @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")
* })
*/
private $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;

/**
* Constructor
*/
public function __construct()
{
$this->team = new \Doctrine\Common\Collections\ArrayCollection();
$this->competition = new \Doctrine\Common\Collections\ArrayCollection();
}


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

return $this;
}

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

/**
* 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 userPassword
*
* @param string $userPassword
* @return Users
*/
public function setUserPassword($userPassword)
{
$this->userPassword = $userPassword;

return $this;
}

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

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

return $this;
}

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

/**
* 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 setRole(\VolleyScout\VolleyScoutBundle\Entity\Roles $role = null)
{
$this->role = $role;

return $this;
}

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

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

我的玩家实体类:

/**
* 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 \DateTime
*
* @ORM\Column(name="player_birthyear", type="datetime", 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 \DateTime $playerBirthyear
* @return Players
*/
public function setPlayerBirthyear($playerBirthyear)
{
$this->playerBirthyear = $playerBirthyear;

return $this;
}

/**
* Get playerBirthyear
*
* @return \DateTime
*/
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;
}
}

当我尝试添加玩家字段时,出现此错误:

Neither the property "player" nor one of the methods "getPlayer()", "isPlayer()", "hasPlayer()", "__get()" exist and have public access in class "VolleyScout\VolleyScoutBundle\Entity\Users".

更新2:
我的玩家表“team_id”中也有一个 FK。因此,当我创建用户时,我还想选择一个团队。所以我想要的是数据库中所有团队的列表,并将其设置为我的表单中的选择字段类型。你知道我如何将其放入我的表单类型中吗?

更新3:

我已将以下内容添加到我的 Users 实体类中:

    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){
$this->player = $player;
}

在我的表单类型中添加了:

->add('player')

现在,在我的表单中,我想要执行以下操作:

{{ form_label(registerForm.player.playerLicensenumber, label|default(),

因为在我的 Players 实体类中,我有playerLicensenumber,但我总是收到此错误:

Method "playerLicensenumber" for object "Symfony\Component\Form\FormView" does not exist in VolleyScoutBundle:User:register.html.twig at line 57

更新4:
这是一个好的解决方案吗:
将其添加到我的注册表单中:

->add('player', new PlayerType())

在我的玩家类型中,我有玩家表中的字段。

最佳答案

您必须为 User 实体中的“player”字段添加一个 setter 和一个 getter。

public function getPlayer() {
return $this->player;
}

public function setPlayer(\Path\to\player\entity $player){
$this->player = $player;
}

对于表单中的团队列表,您必须添加“实体”字段类型。以下是有关更多信息的文档:http://symfony.com/doc/current/reference/forms/types/entity.html

关于php - 从其他表添加数据到表单+ Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21041998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com