Symfony 外键(我如何编辑它们?)
Symfony foreign keys (How can i edit them ?)
我有一个小问题,我不知道为什么我不能编辑我的外键($idProfile),但我可以编辑数据库中的其他值:
我可以更改值 enable 例如但不能更改 idProfile 当我尝试更改它时出现此错误:"An instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface must be injected in FOS\RestBundle\View\ViewHandler to render templates." 不知道这是否可以帮助 thx 寻求帮助:p
总的来说:我只想知道如何使用我的个人资料实体
将我的 $id_profile 编辑为 link
我的配置文件控制器:
/**
* Creates a new profile entity.
*
* @Route("/new/{id}", name="profile_new")
*/
public function newProfileAction(Request $request, User $user)
{
$loggedAs = $this->getUser();
$username = $loggedAs->getUsername();
$profile = new Profile();
$form = $this->createForm(ProfileType::class, $profile);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$profile->setLastConnexion(new \DateTime('now'));
$profile->setCreatedAccount(new \DateTime('now'));
$em = $this->getDoctrine()->getManager();
$em->persist($profile);
$em->flush();
$user->setEnabled('1');
$user->setIdLocation($profile->getId());
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('user_list');
}
return $this->render('admin/user/new_profile.html.twig', array(
'profile' => $profile,
'form' => $form->createView(),
'username' => $username,
));
}
我的用户实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* @ORM\Table(name="user", uniqueConstraints=
{@ORM\UniqueConstraint(name="user_id_uindex", columns={"id"}),
@ORM\UniqueConstraint(name="user_username_uindex", columns=
{"username"})}, indexes={@ORM\Index(name="user_profile_id_fk",
columns={"id_profile"}), @ORM\Index(name="user_localisation_id_fk",
columns={"id_location"})})
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @UniqueEntity("username", groups={"Default", "Patch"})
*/
class User implements UserInterface
{
const ROLE_USER = 'ROLE_USER';
const ROLE_ADMIN = 'ROLE_ADMIN';
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @Serializer\Groups({"Default", "Deserialize", "user_detail"})
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=32)
* @Serializer\Groups({"Default", "Deserialize", "user_detail"})
*/
protected $username;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
* @Serializer\Groups({"Deserialize", "user_detail"})
* @Assert\Regex(
* pattern="/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{7,}/",
* message="Password must be seven characters long and contain at least one digit code, upper case, and lower case letter!",
* groups={"Default", "Patch"}
* )
*/
protected $password;
/**
* @var boolean
*
* @ORM\Column(name="enabled", type="boolean", nullable=false)
*/
protected $enabled = '1';
/**
* @var \AppBundle\Entity\Localisation
*
* @ORM\ManyToOne(targetEntity="Localisation")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_location", referencedColumnName="id")
* })
*/
protected $idLocation;
/**
* @var \AppBundle\Entity\Profile
*
* @ORM\ManyToOne(targetEntity="Profile")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_profile", referencedColumnName="id")
* })
*/
protected $idProfile;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="User", inversedBy="idUserOne")
* @ORM\JoinTable(name="friend",
* joinColumns={
* @ORM\JoinColumn(name="id_user_one", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="id_user_two", referencedColumnName="id")
* }
* )
*/
protected $idUserTwo;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Place", inversedBy="idUserInvited")
* @ORM\JoinTable(name="list_invited",
* joinColumns={
* @ORM\JoinColumn(name="id_user_invited", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="id_place_invited", referencedColumnName="id")
* }
* )
*/
protected $idPlaceInvited;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Place", mappedBy="idUserPresent")
*/
protected $idPlacePresent;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Place", inversedBy="idUserPlace")
* @ORM\JoinTable(name="user_place",
* joinColumns={
* @ORM\JoinColumn(name="id_user_place", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="id_place_place", referencedColumnName="id")
* }
* )
*/
protected $idPlacePlace;
/**
* @var array
* @ORM\Column(type="simple_array", length=200)
* @Serializer\Exclude()
*/
protected $roles;
/**
* Constructor
*/
public function __construct()
{
$this->idUserTwo = new \Doctrine\Common\Collections\ArrayCollection();
$this->idPlaceInvited = new \Doctrine\Common\Collections\ArrayCollection();
$this->idPlacePresent = new \Doctrine\Common\Collections\ArrayCollection();
$this->idPlacePlace = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id)
{
$this->id = $id;
}
/**
* @return string|null
*/
public function getUsername()
{
return $this->username;
}
/**
* @param string $username
*/
public function setUsername(string $username)
{
$this->username = $username;
}
/**
* @return string|null
*/
public function getPassword()
{
return $this->password;
}
/**
* @param string $password
*/
public function setPassword(string $password)
{
$this->password = $password;
}
/**
* @return bool
*/
public function isEnabled()
{
return $this->enabled;
}
/**
* @param bool $enabled
*/
public function setEnabled(bool $enabled)
{
$this->enabled = $enabled;
}
/**
* @return mixed
*/
public function getIdLocation()
{
return $this->idLocation;
}
/**
* @param mixed $idLocation
*/
public function setIdLocation($idLocation)
{
$this->idLocation = $idLocation;
}
/**
* @return mixed
*/
public function getIdProfile()
{
return $this->idProfile;
}
/**
* @param mixed $idProfile
*/
public function setIdProfile($idProfile)
{
$this->idProfile = $idProfile;
}
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getIdUserTwo()
{
return $this->idUserTwo;
}
/**
* @param \Doctrine\Common\Collections\Collection $idUserTwo
*/
public function setIdUserTwo(\Doctrine\Common\Collections\Collection $idUserTwo)
{
$this->idUserTwo = $idUserTwo;
}
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getIdPlaceInvited()
{
return $this->idPlaceInvited;
}
/**
* @param \Doctrine\Common\Collections\Collection $idPlaceInvited
*/
public function setIdPlaceInvited(\Doctrine\Common\Collections\Collection $idPlaceInvited)
{
$this->idPlaceInvited = $idPlaceInvited;
}
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getIdPlacePresent()
{
return $this->idPlacePresent;
}
/**
* @param \Doctrine\Common\Collections\Collection $idPlacePresent
*/
public function setIdPlacePresent(\Doctrine\Common\Collections\Collection $idPlacePresent)
{
$this->idPlacePresent = $idPlacePresent;
}
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getIdPlacePlace()
{
return $this->idPlacePlace;
}
/**
* @param \Doctrine\Common\Collections\Collection $idPlacePlace
*/
public function setIdPlacePlace(\Doctrine\Common\Collections\Collection $idPlacePlace)
{
$this->idPlacePlace = $idPlacePlace;
}
/**
* Returns the roles granted to the user.
*
* <code>
* public function getRoles()
* {
* return array('ROLE_USER');
* }
* </code>
*
* Alternatively, the roles might be stored on a ``roles`` property,
* and populated in any number of different ways when the user object
* is created.
*
* @return (Role|string)[] The user roles
*/
public function getRoles()
{
return $this->roles;
}
/**
* @param array $roles
*/
public function setRoles(array $roles)
{
$this->roles = $roles;
}
/**
* Returns the salt that was originally used to encode the password.
*
* This can return null if the password was not encoded using a salt.
*
* @return string|null The salt
*/
public function getSalt()
{
// TODO: Implement getSalt() method.
}
/**
* Removes sensitive data from the user.
*
* This is important if, at any given point, sensitive information like
* the plain-text password is stored on this object.
*/
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
}
数据库部分画面:[https://ibb.co/iCSnWy
p
[https://ibb.co/cZZedd
p
您将配置文件定义为 \AppBundle\Entity\Profile 的类型,这是使用学说的正确方法。不了解您的实体和数据库有什么区别......
在实体中,您需要设置实体而不是 id。然后,Doctrine 会将 id 传递到数据库中,当您从 doctrine 获取实体时,它会将 id 解析为一个实体。
这意味着:
$user->setIdLocation($profile); //<-- set the entity
$profil = $user->getIdLocation(); // get the profile not the id
我有一个小问题,我不知道为什么我不能编辑我的外键($idProfile),但我可以编辑数据库中的其他值:
我可以更改值 enable 例如但不能更改 idProfile 当我尝试更改它时出现此错误:"An instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface must be injected in FOS\RestBundle\View\ViewHandler to render templates." 不知道这是否可以帮助 thx 寻求帮助:p
总的来说:我只想知道如何使用我的个人资料实体
将我的 $id_profile 编辑为 link我的配置文件控制器:
/**
* Creates a new profile entity.
*
* @Route("/new/{id}", name="profile_new")
*/
public function newProfileAction(Request $request, User $user)
{
$loggedAs = $this->getUser();
$username = $loggedAs->getUsername();
$profile = new Profile();
$form = $this->createForm(ProfileType::class, $profile);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$profile->setLastConnexion(new \DateTime('now'));
$profile->setCreatedAccount(new \DateTime('now'));
$em = $this->getDoctrine()->getManager();
$em->persist($profile);
$em->flush();
$user->setEnabled('1');
$user->setIdLocation($profile->getId());
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('user_list');
}
return $this->render('admin/user/new_profile.html.twig', array(
'profile' => $profile,
'form' => $form->createView(),
'username' => $username,
));
}
我的用户实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* @ORM\Table(name="user", uniqueConstraints=
{@ORM\UniqueConstraint(name="user_id_uindex", columns={"id"}),
@ORM\UniqueConstraint(name="user_username_uindex", columns=
{"username"})}, indexes={@ORM\Index(name="user_profile_id_fk",
columns={"id_profile"}), @ORM\Index(name="user_localisation_id_fk",
columns={"id_location"})})
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @UniqueEntity("username", groups={"Default", "Patch"})
*/
class User implements UserInterface
{
const ROLE_USER = 'ROLE_USER';
const ROLE_ADMIN = 'ROLE_ADMIN';
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @Serializer\Groups({"Default", "Deserialize", "user_detail"})
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=32)
* @Serializer\Groups({"Default", "Deserialize", "user_detail"})
*/
protected $username;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
* @Serializer\Groups({"Deserialize", "user_detail"})
* @Assert\Regex(
* pattern="/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{7,}/",
* message="Password must be seven characters long and contain at least one digit code, upper case, and lower case letter!",
* groups={"Default", "Patch"}
* )
*/
protected $password;
/**
* @var boolean
*
* @ORM\Column(name="enabled", type="boolean", nullable=false)
*/
protected $enabled = '1';
/**
* @var \AppBundle\Entity\Localisation
*
* @ORM\ManyToOne(targetEntity="Localisation")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_location", referencedColumnName="id")
* })
*/
protected $idLocation;
/**
* @var \AppBundle\Entity\Profile
*
* @ORM\ManyToOne(targetEntity="Profile")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_profile", referencedColumnName="id")
* })
*/
protected $idProfile;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="User", inversedBy="idUserOne")
* @ORM\JoinTable(name="friend",
* joinColumns={
* @ORM\JoinColumn(name="id_user_one", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="id_user_two", referencedColumnName="id")
* }
* )
*/
protected $idUserTwo;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Place", inversedBy="idUserInvited")
* @ORM\JoinTable(name="list_invited",
* joinColumns={
* @ORM\JoinColumn(name="id_user_invited", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="id_place_invited", referencedColumnName="id")
* }
* )
*/
protected $idPlaceInvited;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Place", mappedBy="idUserPresent")
*/
protected $idPlacePresent;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Place", inversedBy="idUserPlace")
* @ORM\JoinTable(name="user_place",
* joinColumns={
* @ORM\JoinColumn(name="id_user_place", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="id_place_place", referencedColumnName="id")
* }
* )
*/
protected $idPlacePlace;
/**
* @var array
* @ORM\Column(type="simple_array", length=200)
* @Serializer\Exclude()
*/
protected $roles;
/**
* Constructor
*/
public function __construct()
{
$this->idUserTwo = new \Doctrine\Common\Collections\ArrayCollection();
$this->idPlaceInvited = new \Doctrine\Common\Collections\ArrayCollection();
$this->idPlacePresent = new \Doctrine\Common\Collections\ArrayCollection();
$this->idPlacePlace = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id)
{
$this->id = $id;
}
/**
* @return string|null
*/
public function getUsername()
{
return $this->username;
}
/**
* @param string $username
*/
public function setUsername(string $username)
{
$this->username = $username;
}
/**
* @return string|null
*/
public function getPassword()
{
return $this->password;
}
/**
* @param string $password
*/
public function setPassword(string $password)
{
$this->password = $password;
}
/**
* @return bool
*/
public function isEnabled()
{
return $this->enabled;
}
/**
* @param bool $enabled
*/
public function setEnabled(bool $enabled)
{
$this->enabled = $enabled;
}
/**
* @return mixed
*/
public function getIdLocation()
{
return $this->idLocation;
}
/**
* @param mixed $idLocation
*/
public function setIdLocation($idLocation)
{
$this->idLocation = $idLocation;
}
/**
* @return mixed
*/
public function getIdProfile()
{
return $this->idProfile;
}
/**
* @param mixed $idProfile
*/
public function setIdProfile($idProfile)
{
$this->idProfile = $idProfile;
}
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getIdUserTwo()
{
return $this->idUserTwo;
}
/**
* @param \Doctrine\Common\Collections\Collection $idUserTwo
*/
public function setIdUserTwo(\Doctrine\Common\Collections\Collection $idUserTwo)
{
$this->idUserTwo = $idUserTwo;
}
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getIdPlaceInvited()
{
return $this->idPlaceInvited;
}
/**
* @param \Doctrine\Common\Collections\Collection $idPlaceInvited
*/
public function setIdPlaceInvited(\Doctrine\Common\Collections\Collection $idPlaceInvited)
{
$this->idPlaceInvited = $idPlaceInvited;
}
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getIdPlacePresent()
{
return $this->idPlacePresent;
}
/**
* @param \Doctrine\Common\Collections\Collection $idPlacePresent
*/
public function setIdPlacePresent(\Doctrine\Common\Collections\Collection $idPlacePresent)
{
$this->idPlacePresent = $idPlacePresent;
}
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getIdPlacePlace()
{
return $this->idPlacePlace;
}
/**
* @param \Doctrine\Common\Collections\Collection $idPlacePlace
*/
public function setIdPlacePlace(\Doctrine\Common\Collections\Collection $idPlacePlace)
{
$this->idPlacePlace = $idPlacePlace;
}
/**
* Returns the roles granted to the user.
*
* <code>
* public function getRoles()
* {
* return array('ROLE_USER');
* }
* </code>
*
* Alternatively, the roles might be stored on a ``roles`` property,
* and populated in any number of different ways when the user object
* is created.
*
* @return (Role|string)[] The user roles
*/
public function getRoles()
{
return $this->roles;
}
/**
* @param array $roles
*/
public function setRoles(array $roles)
{
$this->roles = $roles;
}
/**
* Returns the salt that was originally used to encode the password.
*
* This can return null if the password was not encoded using a salt.
*
* @return string|null The salt
*/
public function getSalt()
{
// TODO: Implement getSalt() method.
}
/**
* Removes sensitive data from the user.
*
* This is important if, at any given point, sensitive information like
* the plain-text password is stored on this object.
*/
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
}
数据库部分画面:[https://ibb.co/iCSnWy p [https://ibb.co/cZZedd p
您将配置文件定义为 \AppBundle\Entity\Profile 的类型,这是使用学说的正确方法。不了解您的实体和数据库有什么区别...... 在实体中,您需要设置实体而不是 id。然后,Doctrine 会将 id 传递到数据库中,当您从 doctrine 获取实体时,它会将 id 解析为一个实体。 这意味着:
$user->setIdLocation($profile); //<-- set the entity
$profil = $user->getIdLocation(); // get the profile not the id