Php Symfony2.7 FOSUser将用户和用户信息保存到不同的表中
好的,所以我试图在数据库中保存User to users表和User_info表中的用户信息,比如name,姓氏,使用FosUserBundle,现在这些表之间只有一个双向关系,表单正确保存用户和用户信息,但用户信息表中的用户id始终为空。我从4个小时以来一直在尝试这样做,现在我不知道该怎么做 编辑: 我自己管理它,不需要双向关系,将标记文件中的更改 实体用户Php Symfony2.7 FOSUser将用户和用户信息保存到不同的表中,php,fosuserbundle,symfony-2.7,Php,Fosuserbundle,Symfony 2.7,好的,所以我试图在数据库中保存User to users表和User_info表中的用户信息,比如name,姓氏,使用FosUserBundle,现在这些表之间只有一个双向关系,表单正确保存用户和用户信息,但用户信息表中的用户id始终为空。我从4个小时以来一直在尝试这样做,现在我不知道该怎么做 编辑: 我自己管理它,不需要双向关系,将标记文件中的更改 实体用户 <?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as O
<?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;
}
}
和用户信息类型:
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输入添加到表单中,并修复我的表之间的关系,希望它能帮助别人您修复了吗?我对3个表也有同样的问题。@megadruck是的,这是很久以前的事了,但如果我没记错的话,我已经在builder$builder->add('profil','hidden')->add('profil','new UserInfoType(),array('data_class'=>'AppBundle\Entity\UserInfo'))中添加了隐藏的profil输入;并删除了Users类中relation语句中的mappedBy选项
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';
}
}
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';
}
}