好的,所以我试图将User保存到我的数据库中的users表和用户信息,比如name,surname到user_info表,使用FosUserBundle我现在拥有的是这些表之间的OneToOne双向关系,保存用户和用户信息的表单正确但user_info表中的user_id始终为null我试图这样做4小时后现在我不知道该怎么做。
修改: 我自己管理过,不需要双向关系,会标记文件中的更改
实体用户
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Users
* @ORM\Entity(repositoryClass="AppBundle\Entity\UsersRepository")
* @ORM\Table()
* @ORM\Entity
*/
class Users extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
//don't need mappedBy option and that is whole relation statement
/**
* @ORM\OneToOne(targetEntity="UserInfo", //mappedBy="user", cascade={"persist","remove"})
*/
private $profil;
public function __construct(){
parent::__construct();
}
/**
* Set profil
*
* @param \AppBundle\Entity\UserInfo $profil
*
* @return Users
*/
public function setProfil(\AppBundle\Entity\UserInfo $profil = null)
{
$this->profil = $profil;
return $this;
}
/**
* Get profil
*
* @return \AppBundle\Entity\UserInfo
*/
public function getProfil()
{
return $this->profil;
}
}
UserInfo实体
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* UserInfo
*
* @ORM\Table()
* @ORM\Entity
*/
class UserInfo
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="user_name", type="string", length=255)
*/
private $userName;
/**
* @var string
* @ORM\Column(name="user_surname", type="string", length=255)
*/
private $userSurname;
/**
* @var integer
* @ORM\Column(name="blood_donated")
*/
private $bloodDonated;
/**
* Get id
*
* @return integer
*/
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set userName
*
* @param string $userName
*
* @return UserInfo
*/
public function setUserName($userName)
{
$this->userName = $userName;
return $this;
}
/**
* Get userName
*
* @return string
*/
public function getUserName()
{
return $this->userName;
}
/**
* Set userSurname
*
* @param string $userSurname
*
* @return UserInfo
*/
public function setUserSurname($userSurname)
{
$this->userSurname = $userSurname;
return $this;
}
/**
* Get userSurname
*
* @return string
*/
public function getUserSurname()
{
return $this->userSurname;
}
/**
* Set bloodDonated
*
* @param string $bloodDonated
*
* @return UserInfo
*/
public function setBloodDonated($bloodDonated)
{
$this->bloodDonated = $bloodDonated;
return $this;
}
/**
* Get bloodDonated
*
* @return string
*/
public function getBloodDonated()
{
return $this->bloodDonated;
}
}
表格:
RegistrationFormType:
namespace UsersBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use AppBundle\Form\UserInfoType as UserInfoType;
class RegistrationFormType extends BaseType{
public function buildForm(FormBuilderInterface $builder, array $options){
parent::buildForm($builder, $options);
//adding profil hidden field to set profil in table, here whole magic happens at $builder -> add('profil','hidden')
$builder ->add('profil','hidden')
->add('profil', new UserInfoType(), array('data_class' => 'AppBundle\Entity\UserInfo'))
;
}
public function getName()
{
return 'my_user_registration';
}
}
和UserInfoType:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserInfoType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('userName')
->add('userSurname')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\UserInfo'
));
}
/**
* @return string
*/
public function getName()
{
return 'appbundle_userinfo';
}
}
编辑所以我需要2个更改实际添加profil输入以形成并修复表之间的关系,希望它能帮助某人