Symfony2 Doctrine2 make a managed entity unmanaged - symfony

If I have multiple managed entities and there is a single entity I don't want it to flush how do I make it unmanaged?

You can detach an entity:
$EntityManager->detach($entity);
But, in most cases, this is not necessary. If you don't explicitly call persist on the entity, modifications won't be stored anyway.

Related

Saving a newly created entity with default values from config file that are immutable for subsequent persists

I have a requirement, using Doctrine to save an entity with preset values that are populated from application configuration.
There are multiple applications, sharing same codebase, with different configurations.
My initial idea was to use a pre-persist listener, load config values and be done, but the problem is, these attributes should NOT be changed after entity is created, even if the configuration file changes the original entity's configuration needs to stay as they were initially. Even if entity is loaded in a form and resaved with other values changed, these specific initial applicatin settings should only be set on entity creation and kept for the entire lifecycle of the entiy as they were at the start.
I understand, it is possible to set the values on the entity with the setters of the entity, but since these entities are created at various places in the application my thought was that using a pre-persist hook would reduce code duplication.
Is there a way to detect if an entity is newly created, or another listener I could use? I skimmed through the documentation and did not see listeners specific to object creation.
I thought maybe on first pre-persist, some entity attributes such as "created" is possibly not set on first pre-persist called, when object is created and persisted for the first time.
Is there a way to achieve what I am trying to do, some way to set values on entity creation that is immutable over subsequent persists?
You are on the right way. Of course you can use the prePersist event as they pointed out in the docs
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/events.html#lifecycle-events
prePersist - The prePersist event occurs for a given entity before the
respective EntityManager persist operation for that entity is
executed. It should be noted that this event is only triggered on
initial persist of an entity (i.e. it does not trigger on future
updates).
I would also add a constraint to the entity, that saving your entity with empty value for your desired fields is not possible. So it's ensured that there is no inconsistency.

doctrine:schema:update wants to create an already existing view in symfony 2.8

I have an existing view in my SQL database "view_account" which represents a account entity. This view is read only and has no primary_key field. Actually it has a primary key "id" but the field is not declared as primary key. I can also not change the table design, because its an automatic export from another application generating this tables (the automatic export is every week).
But thats not the problem cause doctrine don't care about "primary key" flag in the database until you don't update the schema.
But whenever i try to "doctrine:schema:update --force" doctrine wants to create this table. How can i ignore this view (or better the entity) from updating by doctrine? Marking the entity read_only with doctrine annotation is not working. Extending a own update-command will also not work, as i found out, it will not work since doctrine 2.4.7 to extend the doctrine-schema-update command (the update command method is ignored in any way).
Whats the best solution?
Edit:
I also tried to configure two entitymanager. One for the "internal" entities and for the "foreign" entities. Since all entities are linked to each other on some point, it is also not possible to have two separated manager don't knowing from each other entities.

Symfony clone an entity from one entity manager to another with FosUser

I want to copy data from one database to another. I get two Customers from two different entity managers (emLocal,emRemote).
The customer entity is in a oneToOne relationship with FosUser
The flush try to insert a blank user. even if I try to detach concerned entities and set every relation to null.
I read from github that FosUserBundle is not designed to works with two entity manager
$remoteCustomer = $order->getCustomer();
$onlineCustomer = $this->emLocal->getRepository("LilWorksStoreBundle:Customer")->findOneBy(array("remoteUser"=>$remoteCustomer->getUser()->getId()));
if(!$onlineCustomer){
$onlineCustomer = clone $remoteCustomer;
}
$this->emLocal->detach($onlineCustomer->getUser());
$onlineCustomer->getUser()->setCustomer(null);
$onlineCustomer->setUser(null);
$this->emLocal->persist($onlineCustomer);
$this->emLocal->flush();
How I can prevent this INSERT?
I believe you should use another Entity Manager to save $onlineCustomer.
$this->emOnline->persist($onlineCustomer);
$this->emOnline->flush();

How to detect if entity exist in database

I have 2 entities, User and Profile. Profile has in-symfony relation with User, but there is no in-database relation (no foreign key, no cascade) - only simple int column named user_id and nothing more.
Problem is obvious: when i delete user - associated profiles persists, but their user_id points to non-existing user row.
Since I use in-symfony relations when i fetch profile from database it fetches also related user entity. I expected that if there is no row with specific ID, it would just leave null or at least throw an exception or something.
Problem is that symfony creates empty User entity object with only id set. rest of its fields are null.
I know solution would be to create FK, constraints etc ... but I'm not allowed to touch anything in database schema.
How can I manage this problem ? I could even leave those empty object if only i had simple way to determine if they exist in database inside TWIG - so i would know if i can display {{ profile.user.email }} for example.
Fast and dirty solution, as you ask, is to use this test: http://twig.sensiolabs.org/doc/tests/defined.html
But I strongly recommend to rework your entity relations.
Found solution: its fetch: EAGER set to problematic mapping in doctrine.
By default doctrine uses LAZY fetching what results in using Proxy classes generated by doctrine for related entity. That class is almost same as real entity class. Difference is inside getter methods that before returning value performs fetching entity from database.
At this point, when you call getter on such proxy, doctrine tries to find entity in database using its ID, and since it doesn't find anything it throws exception.
When using EAGER fetching doctrine performs fetching of related entities on the same time when it fetches main entity and if it doesn't find it then sets null on relation field.

How to log an entity that has collections?

I want to log all changes of an entity. I looked into Loggable doctrine extension as provided by the StofDoctrineExtensionsBundle.
I got it working for fields that store simple data, e.g. string and integers. But my entity also has ManyToMany relationship to another entity, e.g. Tags.
I am getting this error:
InvalidMappingException: Cannot versioned [tags] as it is collection in object - Hn\AssetDbBundle\Entity\Asset
Is there a way to log an entity with its relationships? I don't mind switching to another bundle.
Currently no bundles/extensions have this functionality out of the box. One option would be to implement it yourself. This can be done by making use of Doctrine Listeners. Particularly you need to listen to postUpdate and postPersist events - these happen when entity is updated and created and store your Tags there.
Another option is to get rid of ManyToMany relationship. For this create an intermediate entity AssetTag that would have OneToMany relationship to both Asset and Tag. After this is done, you can use EntityAudit Doctrine Extension, which supports this type of relationships.

Resources