我有 2 个用外键连接的表(user_id 在表中)。我可以通过执行 1 个表单将数据添加到两个表中。但在执行时,我希望外键会在人员表中自动生成。
这是我的实体
用户
namespace Geo\CityTroopersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Geo\CityTroopersBundle\Entity\Users
*
* @ORM\Table(name="users")
* @ORM\Entity
*/
class Users
{
//Vanaf hier bijgevoegd
protected $adress;
protected $person;
//Einde zelf toegevoegde properties
/**
* @var integer $userId
*
* @ORM\Column(name="user_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userId;
/**
* @var string $user
*
* @ORM\Column(name="user_password", type="string", length=128, nullable=true)
*/
private $userPassword;
/**
* @var string $userEmail
*
* @ORM\Column(name="user_email", type="string", length=128, nullable=true)
*/
private $userEmail;
/**
* @var string $userImage
*
* @ORM\Column(name="user_image", type="string", length=128, nullable=true)
*/
private $userImage;
/**
* @var string $userQuestion
*
* @ORM\Column(name="user_question", type="string", length=128, nullable=true)
*/
private $userQuestion;
/**
* @var string $userAnswer
*
* @ORM\Column(name="user_answer", type="string", length=128, nullable=true)
*/
private $userAnswer;
/**
* @var \DateTime $userCreateddate
*
* @ORM\Column(name="user_createddate", type="datetime", nullable=true)
*/
/**
* Get userId
*
* @return integer
*/
public function getUserId()
{
return $this->userId;
}
/**
* 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 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 userImage
*
* @param string $userImage
* @return Users
*/
public function setUserImage($userImage)
{
$this->userImage = $userImage;
return $this;
}
/**
* Get userImage
*
* @return string
*/
public function getUserImage()
{
return $this->userImage;
}
/**
* Set userQuestion
*
* @param string $userQuestion
* @return Users
*/
public function setUserQuestion($userQuestion)
{
$this->userQuestion = $userQuestion;
return $this;
}
/**
* Get userQuestion
*
* @return string
*/
public function getUserQuestion()
{
return $this->userQuestion;
}
/**
* Set userAnswer
*
* @param string $userAnswer
* @return Users
*/
public function setUserAnswer($userAnswer)
{
$this->userAnswer = $userAnswer;
return $this;
}
/**
* Get userAnswer
*
* @return string
*/
public function getUserAnswer()
{
return $this->userAnswer;
}
//Zelf toegevoegde getters & setters
public function getPerson()
{
return $this->person;
}
public function setPerson(Persons $person = null)
{
$this->person = $person;
}
public function getAdress()
{
return $this->adress;
}
public function setAdress(Adresses $adress = null)
{
$this->adress = $adress;
}
}
人员
namespace Geo\CityTroopersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Geo\CityTroopersBundle\Entity\Persons
*
* @ORM\Table(name="persons")
* @ORM\Entity
*/
class Persons
{
/**
* @var integer $personId
*
* @ORM\Column(name="person_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $personId;
/**
* @var integer $adressId
*
* @ORM\Column(name="adress_id", type="integer", nullable=false)
*/
private $adressId;
/**
* @var string $personFamilyname
*
* @ORM\Column(name="person_familyname", type="string", length=64, nullable=false)
*/
private $personFamilyname;
/**
* @var string $personFirstname
*
* @ORM\Column(name="person_firstname", type="string", length=64, nullable=false)
*/
private $personFirstname;
/**
* @var boolean $personGender
*
* @ORM\Column(name="person_gender", type="boolean", nullable=false)
*/
private $personGender;
/**
* @var string $personNationality
*
* @ORM\Column(name="person_nationality", type="string", length=45, nullable=false)
*/
private $personNationality;
/**
* @var Users
*
* @ORM\ManyToOne(targetEntity="Users")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Get personId
*
* @return integer
*/
public function getPersonId()
{
return $this->personId;
}
/**
* Set adressId
*
* @param integer $adressId
* @return Persons
*/
public function setAdressId($adressId)
{
$this->adressId = $adressId;
return $this;
}
/**
* Get adressId
*
* @return integer
*/
public function getAdressId()
{
return $this->adressId;
}
/**
* Set personFamilyname
*
* @param string $personFamilyname
* @return Persons
*/
public function setPersonFamilyname($personFamilyname)
{
$this->personFamilyname = $personFamilyname;
return $this;
}
/**
* Get personFamilyname
*
* @return string
*/
public function getPersonFamilyname()
{
return $this->personFamilyname;
}
/**
* Set personFirstname
*
* @param string $personFirstname
* @return Persons
*/
public function setPersonFirstname($personFirstname)
{
$this->personFirstname = $personFirstname;
return $this;
}
/**
* Get personFirstname
*
* @return string
*/
public function getPersonFirstname()
{
return $this->personFirstname;
}
/**
* Set personGender
*
* @param boolean $personGender
* @return Persons
*/
public function setPersonGender($personGender)
{
$this->personGender = $personGender;
return $this;
}
/**
* Get personGender
*
* @return boolean
*/
public function getPersonGender()
{
return $this->personGender;
}
/**
* Set personNationality
*
* @param string $personNationality
* @return Persons
*/
public function setPersonNationality($personNationality)
{
$this->personNationality = $personNationality;
return $this;
}
/**
* Get personNationality
*
* @return string
*/
public function getPersonNationality()
{
return $this->personNationality;
}
/**
* Set user
*
* @param Geo\CityTroopersBundle\Entity\Users $user
* @return Persons
*/
public function setUser(\Geo\CityTroopersBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return Geo\CityTroopersBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
}
控制器
class UserController extends Controller
{
public function registerAction(Request $request)
{
$user = new Users();
$person = new Persons();
//$adress = new Adresses();
$form = $this->createForm(new UserType(), $user);
if ($request->getMethod() == 'POST'){
$form->bindRequest($request);
if($form->isValid()){
$person = $form->getData();
//$adress = $form->getData();
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->persist($user->getPerson());
//$em->persist($user->getAdress());
$em->flush();
return $this->redirect($this->generateUrl('indexpage'));
}
}
return $this->render('GeoCityTroopersBundle:User:register.html.twig', array(
'form' => $form->createView(),
));
}
}
用户类型
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('person', new PersonType());
$builder->add('userEmail');
$builder->add('userPassword', 'password');
$builder->add('userImage');
$builder->add('userQuestion');
$builder->add('userAnswer');
//$builder->add('adress', new AdressType());
}
public function getName()
{
return 'user';
}
}
人型
class PersonType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('personFamilyname');
$builder->add('personFirstname');
$builder->add('personNationality');
$builder->add('personGender');
//$builder->add('adressId');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Geo\CityTroopersBundle\Entity\Persons',
);
}
public function getName()
{
return 'person';
}
}
我试图在用户的注释中添加以下内容:
/**
* Bidirectional (INVERSE SIDE)
* @ORM\OneToOne(targetEntity="Persons",cascade={"persist"})
*
*/
但这也没有奏效。我感觉我的数据库设置有问题,人和用户之间的关系是一对一的。
更新:删除不必要的
提前致谢