Reputation: 1500
I have two entities: User and UserProfile. At user entity primary key is autoincrement and named intUserID. UserProfile entity have the same primary key (but not autoincrement) and associated with User entity. When i'm trying to insert all user data to this entities, doctrine throws an exception: Entity of type Project\UsersBundle\Entity\UserProfile is missing an assigned ID for field 'intuserid'
YML mapping for UserEntity:
Project\UsersBundle\Entity\User:
type: entity
table: user
fields:
intuserid:
id: true
type: integer
unsigned: true
nullable: false
column: intUserID
generator:
strategy: IDENTITY
varname:
type: string
length: 150
fixed: false
nullable: false
column: varName
varemail:
type: string
length: 150
fixed: false
nullable: false
column: varEmail
varpassword:
type: string
length: 40
fixed: false
nullable: false
column: varPassword
oneToOne:
profile:
targetEntity: Project\UsersBundle\Entity\UserProfile
cascade: ["all"]
joinColumn:
name: intuserid
referencedColumnName: intUserID
lifecycleCallbacks: { }
YML form UserProfile:
Project\UsersBundle\Entity\UserProfile:
type: entity
table: user_profile
fields:
intuserid:
id: true
type: integer
unsigned: true
nullable: false
column: intUserID
varsurname:
type: string
length: 50
fixed: false
nullable: true
column: varSurname
varpatronymic:
type: string
length: 50
fixed: false
nullable: true
column: varPatronymic
oneToOne:
user:
targetEntity: Project\UsersBundle\Entity\User
inversedBy: profile
joinColumn:
name: intuserid
referencedColumnName: intUserID
lifecycleCallbacks: { }
CreateAction:
public function createAction(Request $request)
{
$user = new User();
$form = $this->createCreateForm($user);
$form->handleRequest($request);
if ($form->isValid())
{
$user->setVarSalt(md5(microtime().$user->getVarEmail()));
if ($user->getVarPassword())
{
$encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
$password = $encoder->encodePassword($user->getPassword(), $user->getSalt());
$user->setVarPassword($password);
}
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$this->get('session')->getFlashBag()->add('success', 'Successfully saved');
return $this->redirect($this->generateUrl('users_edit', array('id' => $user->getIntUserID())));
}
return $this->render('ProjectUsersBundle:Default:edit.html.twig', array(
'entity' => $user,
'form' => $form->createView(),
));
}
Form User
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('varname', 'text', array('label' => 'Название', 'required' => true))
->add('varemail', 'text', array('label' => 'E-mail', 'required' => true))
->add('varpassword', 'password', array('label' => 'Пароль'))
->add('isactive', 'checkbox', array('label' => 'Активен'))
->add('ispublic', 'checkbox', array('label' => 'Публичный профиль'))
->add('profile', new UsersProfileType())
->add('save', 'submit', array('label' => 'Сохранить'));
}
Form UserProfile
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('varsurname', 'text', array('label' => 'Фамилия'))
->add('varpatronymic', 'text', array('label' => 'Отчество'))
->add('save', 'submit', array('label' => 'Сохранить'));
}
Upvotes: 1
Views: 1343
Reputation: 6606
You have declared the field intuserid
twice in your UserProfile
entity. Remove the first declaration:
# remove this part
oneToOne:
user:
targetEntity: Project\UsersBundle\Entity\User
inversedBy: profile
joinColumn:
name: intuserid
referencedColumnName: intUserID
You also may need to remove the UserProfile
before persisting User
.
// set the UserProfile to null (assumes null default value to setUserProfile)
$profile = $user->getUserProfile();
$user->setUserProfile();
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$profile->setIntuserid($user->getIntuserid());
$em->persist($profile);
$em->flush();
Upvotes: 1