Symfony外键(如何编辑?)

时间:2018-07-20 13:51:20

标签: symfony doctrine foreign-keys relational-database

我有一个小问题,我不知道为什么我不能编辑外键($ idProfile),但是我可以从数据库中编辑另一个值:

当我尝试更改值时,我可以更改示例的启用值,但不能更改idProfile,但出现以下错误:“必须在FOS \ RestBundle \ View \ ViewHandler中注入Symfony \ Bundle \ FrameworkBundle \ Templating \ EngineInterface的实例呈现模板。”不知道这是否可以帮助帮助人员:p

一般而言:我只想知道如何编辑$ id_profile使其与个人资料实体链接

我的配置文件控制器:

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

我的userEntity:

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

1 个答案:

答案 0 :(得分:0)

您将配置文件定义为\ AppBundle \ Entity \ Profile的类型,这是使用原则的正确方法。不了解您的实体和数据库有什么区别... 在实体中,您需要设置实体而不是ID。然后,Doctrine将在数据库中传递ID,并且当您从学说中获取实体时,它将ID解析为一个实体。 这意味着:

 $user->setIdLocation($profile); //<-- set the entity
$profil =  $user->getIdLocation(); // get the profile not the id