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 7437267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:22:21+00:00 2026-05-29T10:22:21+00:00

I have three Doctrine entities: Device, which has a OneToOne relationship with Device\Status, which

  • 0

I have three Doctrine entities: Device, which has a OneToOne relationship with Device\Status, which in turn has a OneToOne relationship with Device\Status\Battery.

I have {cascade=”persist”} set between the related entities, and from what I’ve read, that should be all that is required for Doctrine to automatically persist each of the entities without having to do anything myself in the code.

Here’s what I’m having problems with:

$device = new \Entities\Device();
$device->setId(100);

$status = $device->getStatus();
$status->setIpAddress('192.168.0.1');

$battery = $status->getBattery();
$battery->setInternalLevel(60);

$em->persist($device);
$em->flush();

After executing this code, I get the following error:

Entity of type Device\Status\Battery has identity through a foreign entity 
Device\Status, however this entity has no identity itself. You have to call 
EntityManager#persist() on the related entity and make sure that an identifier 
was generated before trying to persist 'Device\Status\Battery'. In case of 
Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) 
this means you have to call EntityManager#flush() between both persist 
operations.

My question is: what is the correct way to setup my entities to ensure that they’re persisted in the correct order?

The code for the entities can be found here: https://gist.github.com/1753524

All tests have been performed using the Doctrine 2.2 sandbox.

  • 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-05-29T10:22:22+00:00Added an answer on May 29, 2026 at 10:22 am

    I think @CappY is right.

    The problem is in the Status entity. when you do getBattery() and create a new Battery instance, it’s related to the Status instance on which you called getBattery().

    Since that instance hasn’t been stored in the database yet, it’s id hasn’t been generated (because it’s annotated as @GeneratedValue). you’re almost right about cascade persist. except for that it’s performed in memory.

    So you need to persist and flush Status entity before doing getBattery() if you want to use that entity as id in Battery. Or else you could simple add an id field for Battery 🙂

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

Sidebar

Related Questions

I'm using Doctrine 2 ORM and I have this problem. I have three Entities,
I have three entities: User, Office and PhoneNumber. The user has many phone numbers,
I have three (C++) classes: Player, Hand, and Card. Player has a member, hand,
Using Doctrine PHP If I have a user with a many to many relationship
I am using Doctrine 2 and Zend Framework 1.11. I have set up my
I have a bi-directional OneToMany relationship using Doctrine 2 as an ORM within the
Using Docrine2 entities, I have a list entity, with a manytomany relationship with item.
In my Symfony2 project, I have a ModelBundle which holds my entities, and other
I have three entities: User , Answer , and Question . There is a
I have three tables: page, attachment, page-attachment I have data like this: page ID

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.