Doctrine2 - using a foreign key column as a normal field

Is there a way to learn something like this in the 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 am doing something like this:

call Entity :: setRelatedEntityId ($ someId) and save the object, and the entity returns the associated object by calling Entity :: getRelatedEntity ().

A related object is selected from a table that will be strictly limited and will never dynamically grow at run time, so there are a finite number of identifiers for related objects.

When creating a new Entity, I would like to set the identifier of the related object, but without the need to retrieve the entire associated object from the database.

As far as I can verify this, this will not work, because if I set the related EntityId but not the related element, Doctrine automatically sets the related_entity_id column to null, because in principle the relationship is not established.

I also tried to do something like this:

remove the relatedEntityId property and use

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

the constructor of the RelatedEntity function will set id, but not other values. I donโ€™t want the persistence of a bound Entity (these values โ€‹โ€‹are already set in the database for the given $ relEntId), but this time Doctrine signals a flash error because it has an uncertified object.

Basically, I want to create a relationship, not knowing anyhing, but the identifier of the associated object. If there is another way, this can be done, please share.

Thanks in advance

EDIT:

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

  • use entityManager to find all related items;
  • enter a list to the object that will create new objects
  • when creating a new object, select one of the events associated with it from the list as an element associated with it

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

+4
source share
3 answers

Use proxy server:

 Entity::setRelatedEntity($entityManager->getReference('RelatedEntity', $relEntId)) 
+4
source

I donโ€™t think this should work as you described :)

The object you add must be a Doctrine managed object, so you must first load it yourself using the find() family of methods.

+1
source

Based on my experience with Doctrine 2, described in more detail here http://ssmusoke.wordpress.com/2012/03/25/doctrine2-day-3-proxies-associations-relationships/

My approach is as follows:

a) Have only the $ relatedEntity property

b) Add a getRelatedEntityId () function that returns the id value of $ relatedEntity

c) Add setRelatedEntityId (), which sets the $ relatedEntity object - you may need to download it from the database, it saves you from polluting other layers when u has only the identifier of the associated object

d) Add functions getRelatedEntity () and setRelatedEntity ()

BOTTOM LINE: you cannot have properties for a foreign key column, but a displayed property, because Doctrine gets confused

0
source

Source: https://habr.com/ru/post/1414676/


All Articles