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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:01:15+00:00 2026-06-04T12:01:15+00:00

Is there a way to have something like this in doctrine: class Entity {

  • 0

Is there a way to have something like this in doctrine:

class Entity {

    /**
     * @Column(name="related_entity_id")
     */
    private $relatedEntityId;

    /**
     * @ManyToOne(targetEntity="RelatedEntitiy")
     * @JoinColumn(name="related_entity_id", referencedColumnName="id")
     */ 
    private $relatedEntity;
}

What I want to do I do something like this:

call Entity::setRelatedEntityId($someId), and persist the entity,
and have the entity return the related entity by calling Entity::getRelatedEntity().

The related entity is selected from a table which will be strictly limited and it will never dynamically grow at runtime, so there is a finite number of related entity ids.

At the time of creating a new Entity, I’d like to set the related entity id, but without having to fetch the whole related entity from the database.

As far as I could test this, it does not work, because if I set the relatedEntityId but not the relatedEntity, Doctrine automatically sets the related_entity_id column to null, since basically no relationship has been established.

I’ve tried to do something like this also:

remove the relatedEntityId property, and use

Entity::setRelatedEntity(new RelatedEntity($relEntId))

the constructor of the RelatedEntity will set the id, but not other values.
I do not want to persist the RelatedEntity (it’s values are already set in the DB for the given $relEntId), but this time Doctrine signals an error at flush, because it has an unpersisted entity.

Basically, what I want to do is create a relationship without knowing anyhing but the Id of the related entity. If there is some other way this can be done, please share.

Thanks in advance

EDIT:

I’ve found a workaround. Since the RelatedEntities will be a limited set of immutable objects, I’ve done the following:

  1. use the entityManager to find all RelatedEntities;
  2. inject the list to the object that will be creating new Entities
  3. when creating a new Entity, select one of the RelatedEntities from the list as its RelatedEntity

I’ll leave the question open for a day or two, just in case somebody comes up with something better.

  • 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-04T12:01:18+00:00Added an answer on June 4, 2026 at 12:01 pm

    Use the entity proxy:

    Entity::setRelatedEntity($entityManager->getReference('RelatedEntity', $relEntId))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have something like this: File.new('http://hostname/myfile.zip') inside a controller?
If I have something like this: Is there a way to fire the mouseover
Is there a way to modify a UIElement's contents? I have something like this:
is there a way in Excel to have a formula that does something like
i have something like this in my views.py instance = get_object_or_404(register,pk=request.user.id) Now if there
I have something like this: public class Foo { public Bar[] Bars{get; set;} }
I have something like this in my page: <div id=test> <div class=item>Test</div> <div class=item>Test</div>
I have something like this on my core data class to fetch all data
If i have something like this : function foo(class){ for(i=0; i<(class.length);i++){ return document.getElementsByClassName(class)[i]; }
Well, I have something like this: trait A class Serving(a: => A) object App

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.