Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Association mapping not working #2871

Open
bbobhel166 opened this issue Nov 21, 2018 · 0 comments
Open

Association mapping not working #2871

bbobhel166 opened this issue Nov 21, 2018 · 0 comments

Comments

@bbobhel166
Copy link

Symfony FOSUserBundle versions: 2.1.2 with sympfony 4.1

Description of the problem including expected versus actual behavior:

I try to create an mapping between a user and a profile class. l don't succès to load data to the form and save it. I got the following message :
The association App\Entity\Profile#user refers to the owning side field App\Entity\User#profile which does not exist.
Everything else is working perfectly (internationalization, surcharges, and so on.)

Steps to reproduce:

  1. User.php
/**
    * @ORM\OneToOne(targetEntity="Profile", inversedBy="user")
    * @ORM\JoinColumn(name="profile_id", referencedColumnName="id")
    */
   protected $profile;

/**
    * @return mixed
    */
   public function getProfile()
   {
       return $this->profile;
   }

   /**
    * @param mixed $profile
    */
   public function setProfile($profile): void
   {
       $this->profile = $profile;
   }


  1. Profile.php
 /**
     * @ORM\OneToOne(targetEntity="User", mappedBy="profile" )
     */
    protected $user;

/**
     * @return mixed
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param mixed $user
     */
    public function setUser($user): void
    {
        $this->user = $user;
    }
  1. UserFormType.php
class UserFormType extends AbstractType
{

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
            ->add('email', EmailType::class, array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle',
                'attr' => array(
                    'readonly' => true,
                ),
                ))
            ->add('profile', ProfileFormType::class, array('label' => false))
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\Entity\User',
            'csrf_token_id' => 'user',
        ));
    }
    
}
  1. ProfileFormType.php
class ProfileFormType extends AbstractType
{

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('lastname', null, array('label' => 'Lastname', 'translation_domain' => 'profile'))
            ->add('firstname', null, array('label' => 'Firstname', 'translation_domain' => 'profile'))
        ;
    }


    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\Entity\Profile',
            'csrf_token_id' => 'profile',
        ));
    }


}
  1. ProfileController.php
public function profilepage(UserInterface $user, Request $request, EntityManagerInterface $em, $_locale, $locales)
    {
        /** @var $userManager  */
        $userManager = $this->get('fos_user_manager');
        /** @var $formFactory  */
        $formFactory = $this->get('fos_profile_factory');
        /** @var $dispatcher  */
        $dispatcher = $this->get('fos_event_dispatcher');

       $user = $this->getUser();

        if (!is_object($user) || !$user instanceof UserInterface) {
            throw new AccessDeniedException('This user does not have access to this section.');
        }

        $event = new GetResponseUserEvent($user, $request);
        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);

        if (null !== $event->getResponse()) {
            return $event->getResponse();
        }
        
        $form = $formFactory->createForm();
        //$form = $this->createForm(UserFormType::class, $user);
        $form->setData($user);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            $entityManager = $this->getDoctrine()->getManager();
            
            // actually executes the queries (i.e. the INSERT query)
            $entityManager->flush();

            // user
            $event = new FormEvent($form, $request);
            $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
            
            $userManager->updateUser($user);

            if (null === $response = $event->getResponse()) {
                $url = $this->generateUrl('page_profile');
                $response = new RedirectResponse($url);
            }

            $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

            return $response;
        }
        
        return $this->render('@FOSUser/Profile/edit.html.twig', array(
            'form' => $form->createView(),
        ));


    }

Provide logs (if relevant):

Describe the feature:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant