Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8069207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:56:52+00:00 2026-06-05T12:56:52+00:00

I am trying to persist an user entity with a profile entity from a

  • 0

I am trying to persist an user entity with a profile entity from a single form submit. Following the instructions at the Doctrine2 documentation and after adding additional attributes this seemed to be sufficient to achieve the goal.

Entities

Setting up the entites in accordance is pretty straight forward and resulted in this (I left out the generated getter/setter):

// ...

/**
 * @ORM\Entity
 */
class User
{
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue
   */
  private $id;

  /**
   * @ORM\Column(type="string", length=64)
   */
  private $data;

  /**
   * @ORM\OneToOne(targetEntity="Profile", mappedBy="user", cascade={"persist", "remove"})
   */
  private $Profile;

  // ...
}

// ...

/**
 * @ORM\Entity
 */
class Profile
{
  /**
   * @ORM\Id
   * @ORM\OneToOne(targetEntity="User")
   */
  private $user;

  /**
   * @ORM\Column(type="string", length=64)
   */
  private $data;

  // ...
}

Forms

Now modifiying the forms is not too difficult as well:

// ...

class ProfileType extends AbstractType
{
  public function buildForm(FormBuilder $builder, array $options)
  {
      $builder
          ->add('data')
      ;
  }

  public function getName()
  {
      return 'profile';
  }

  public function getDefaultOptions(array $options)
  {
      return array('data_class' => 'Acme\TestBundle\Entity\Profile');
  }
}

// ...

class TestUserType extends AbstractType
{
  public function buildForm(FormBuilder $builder, array $options)
  {
      $builder
          ->add('data')
          ->add('Profile', new ProfileType())
      ;
  }

  public function getName()
  {
      return 'user';
  }
}

Controller

class UserController extends Controller
{
  // ...

  public function newAction()
  {
      $entity = new User();
      $form   = $this->createForm(new UserType(), $entity);

      return array(
          'entity' => $entity,
          'form'   => $form->createView()
      );
  }

  public function createAction()
  {
      $entity  = new User();
      $request = $this->getRequest();
      $form    = $this->createForm(new UserType(), $entity);
      $form->bindRequest($request);

      if ($form->isValid()) {
          $em = $this->getDoctrine()->getEntityManager();
          $em->persist($entity);
          $em->flush();

          return $this->redirect($this->generateUrl('user_show',
              array('id' => $entity->getId())));

      }

      return array(
          'entity' => $entity,
          'form'   => $form->createView()
      );
    }

    // ...
}    

But now comes the part where testing takes place. I start to create a new user-object, the embedded form shows up as expected, but hitting submit returns this:

Exception

Entity of type Acme\TestBundle\Entity\Profile is missing an
assigned ID. The identifier generation strategy for this entity
requires the ID field to be populated before EntityManager#persist()
is called. If you want automatically generated identifiers instead
you need to adjust the metadata mapping accordingly.

A possible solution I am already aware of is to add an additional column for a stand-alone primary key on the Profile entity.

However I wonder if there is a way to keep the mapping roughly the same but deal with persisting the embedded form instead?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:56:54+00:00Added an answer on June 5, 2026 at 12:56 pm

    After debating for quite a while with a couple of people via IRC I modified the mapping and came up with this:

    Entities

    // ...
    
    /**
     * @ORM\Entity
     */
    class User
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @ORM\Column(type="string", length=64)
         */
        private $data;
    
        /**
         * @ORM\OneToOne(targetEntity="Profile", cascade={"persist", "remove"})
         */
        private $Profile;
    
        // ...
    }
    
    // ...
    
    /**
     * @ORM\Entity
     */
    class Profile
    {
        /**
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\Column(type="string", length=64)
         */
        private $data;
    
        // ...
    
    }    
    

    So what does this change? First of all I removed the mappedBy and inversedBy options for the relation. In addition the OneToOne annotation on the Profile-entity was not needed.

    The relation between User and Profile can be bi-directional however a uni-directional relation with User being the owning side is sufficient to have control over the data. Due to the cascade option you can be sure there are no left-over Profiles without Users and Users can maintain a Profile but do not have to.

    If you want to use a bi-directional relation I recommand taking a look at Github: Doctrine2 – Tests – DDC117 and especially pay attention to Article and ArticleDetails’ OneToOne relation. However you need to be aware that saving this bi-directional relation is a bit more tricky as can be seen from the test file (link provided in comment): you need to persist the Article first and setup the constructor in ArticleDetails::__construct accordingly to cover the bi-directional nature of the relationship.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to persist user settings to a configuration file using ConfigurationManager. I want
I am trying to persist an Entity with a @Lob annotated String field. The
I am trying to save an user. The user entity and group entity has
I am trying to persist the location of a window, I have the following
i am trying to update a user table with a single value update, but
I'm trying to set up the following tables using JPA/Hibernate: User: userid - PK
I'm using JPA2 and Hibernate implementation. I'm trying to persist a User object, which
I am trying to get tests working after switching from Webrat to Capybara. When
Problem Context: I am trying to persist a file, uploaded by the user, to
I'm trying to make a user session in CakePHP persist across all subdomains. All

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.