Symfony 4 with JMS.
The Parent class is associated with the OneToMany child class. In the repository, I get some parents, and some children (leftJoin with the condition). However, when trying to serialize Parents, JMS automatically gets all (!) Children from database, making additional queries to the database. How can I avoid this? Is there any option in the JMS config?
The only thing that comes to my mind is the VirtualProperty() version, in which I will filter the children already inside the entity. But this is not very effective.
UPD.
I want to serialize this field, but not all child entities. Only satisfying the condition. In the repository I do ->leftJoin('p.children', 'c', 'WITH', 'c.foo = bar'), but the serializer loads the elements that are not in the condition by separate requests. This is the problem
Use serialization groups to choice which attributs you want and limit childrens with maxDepth ?
Related
I have a rest api with a type "Entity", this Entity has a list of children which themselves are also type Entity
Accessing the resources is as follows:
Get Children of an entity:
GET /api/entities/{id}/children
Get Single entity:
GET /api/entities/{id}
For removing resources I currently have the following planned:
Remove child from parent but keep child resource in database (delete ONLY the relation):
DELETE /api/entities/{id}/children/{childId} (calling GET /api/entities/{childId} will still return this resource!)
Remove entity from database:
DELETE /api/entites/{id}
Is this the common practice in REST API design for differentiating between removing a relationship vs removing a resource? Or is it expected that the child resource will be both removed from the hierarchy and completely deleted as well? What are the common ways to differentiate these two actions?
I can't believe this hasn't come up for other people, but I'm unable to find a solution.
Let's say I have two entity types, A and B with a one-to-many relationship. A has a collection of Bs.
The form for A has a CollectionType for the Bs, with a custom entry_type for B, allow_add and allow_delete set to true. When the form is created/populated/rendered, the Bs' fields are identified by their index in the collection. When the form is posted back, the fields are mapped back onto the B entities according to the index again.
What if the database in the mean time decided to return the Bs in a different order? Then the values get swapped around on the Bs! I can't have that, as other entities will reference the Bs and now they've changed their meaning!
Even if the database doesn't change the order, the same issue appears when a B is deleted: The fields get shifted through the Bs and a different one deleted! (Ok, I'm not a 100% certain this happens, as there's a gap then in the numbering of the posted fields.) I've found this similar question where it does happen when another one is created (Symfony CollectionType regards deletion+creation as a modification of an item), but that sort of drifted from the issue and there's no usable answer.
How do I make sure the form is updating the entities the user actually edited?
I already tried to render the Bs' IDs as a HiddenType, but then the form rightfully complains that the ID has no setter. It would probably force an ID on the wrong B anyways and Doctrine doesn't like that. I suppose I could add the Bs as unmapped and copy the values over to the correct objects manually, but that would defeat a good chunk of Symfony's form system.
I've used CollectionType before, but not for entities that are referenced elsewhere. I would then delete all of the previous entities and create the collection anew from the posted data. But I can't do that now, can I?
Since doctrine 2.1, it's possible to change how associations are indexed. This will allow you to use the id as the collection key (as the field has to be unique):
#OneToMany(targetEntity="B", mappedBy="A", indexBy="id")
You might also need to enable orphanRemoval so that the data is actually removed instead of the relation just set to null.
I've got multiple entities set up with their respective repositories, all working properly. However, Doctrine seems to populate proxies where I don't want them to be populated.
I've got an entity called Item, which references a Category, both by having a $category_id and a $category field. The latter has the relationship set up with #ORM\ManyToOne() and #ORM\JoinColumn(), working correctly.
In my controller, when I'm querying Items, I receive a list of items with proxies to the related categories, which I can strip out from my response, identifying them as being proxies. However, if for whatever reason, I also query Categories in an unrelated query, the item-related query return with not proxies but actual hydrated Category instances, which I don't want.
$this->categoryRepository->findBy(...);
...
$items = $this->itemRepository->findBy(...);
return $this->respond($items);
Here, $items[0]->category will have been populated by the framework by the time the execution reaches the return statement.
Is it possible to turn this behaviour off?
You can specify the fetch policy on a relationship to be EXTRA_LAZY, this will fetch the least amount of date on execution as possible. Per the docs:
With Doctrine 2.1 a feature called Extra Lazy is introduced for associations. Associations are marked as Lazy by default, which means the whole collection object for an association is populated the first time its accessed.
So in your #ORM\ManyToOne() annotation, add a parameter: fetch="EXTRA_LAZY" to the others.
I'm building an app that allows a user to create reports for advertisers. The entities are set up so that there is a relation between the Report object and the Advertiser object - so that the advertiser has a getReports() method to get them.
I would like to change the app so that instead of actually deleting entities, that it simply changes a "deleted" property to true. That part is no problem, but I'm unsure how to make it so that the getReports() on the Advertiser entity only returns reports for the advertiser that have a deleted property of false.
Please let me know if you have any suggestions how that should be done in accordance with Symfony best practices.
You should look into Gedmo Doctrine Extensions. http://atlantic18.github.io/DoctrineExtensions/
Specifically for your case:
http://atlantic18.github.io/DoctrineExtensions/doc/softdeleteable.html
TLDR; they allow you to configure behavior of your entities in a way you desire, so for example when you "delete" an entity, Gedmo's listeners would set it's deleted value to a current datetime. Now you'd still have that record in your database but with not null value of deleted column marking it 'soft deleted', so when querying, it wouldn't be returned (because Doctrine knows how to query these stuff and would add a condition i.e.: ... where deleted ...) unless you explicitly say you want to see those soft deleted records.
I want to make a priority mechanic for a Entity, by default I'll sort everything by priority (not by Id), I'd like for priority to be set on persist (with the Objects Id) so I can switch the order of items on the list and always be pushed as the last one.
What's the best method to assign the priority on the object? using lifecycle callbacks on persist? or is there a quicker method?
TL;DR;
Need variable that is set like id of an object just allows switching 2 elements so I can move one up the list.
If I understand what you wrote correctly, you might want to research the "sortable" doctrine behaviour:
https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/sortable.md