V
V
Victor Diditsky2016-09-20 01:56:59
MySQL
Victor Diditsky, 2016-09-20 01:56:59

How to avoid looping when getting related entities through doctrine?

Hello!
I have 2 entities Category and Product.
They are connected in this way:
A category can have many products (OneToMany)
A product can have only 1 category (ManyToOne)
I encountered such a problem - when getting a product from the database, the request loops

public function search($data)
    {
        $qb = $this->createQueryBuilder("p")
            ->select('p')
            ->where("p.id1c = :data")
            ->setParameter("data", $data);

        $products = $qb->getQuery()
            ->getResult();

        return $products;
    }

2 requests are written to the log:
[2016-09-20 01:28:20] doctrine.DEBUG: SELECT p0_.id AS id_0, p0_.id1c AS id1c_1, p0_.status AS status_2, p0_.limit_sales AS limit_sales_3, p0_.type_of_products AS type_of_products_4, p0_.name AS name_5, p0_.code AS code_6, p0_.description AS description_7, p0_.handler AS handler_8, p0_.art AS art_9, p0_.price AS price_10, p0_.price_retail AS price_retail_11, p0_.image AS image_12, p0_.image_width AS image_width_13, p0_.image_height AS image_height_14, p0_.image_big AS image_big_15, p0_.image_big_width AS image_big_width_16, p0_.image_big_height AS image_big_height_17, p0_.sort AS sort_18, p0_.date_updated AS date_updated_19, p0_.date_created AS date_created_20, p0_.category_id AS category_id_21, p0_.exchange_rate_setting_id AS exchange_rate_setting_id_22 FROM `product` p0_ WHERE p0_.id1c = ? ["00000082457"] []
[2016-09-20 01:28:20] doctrine.DEBUG: SELECT t0.id AS id_1, t0.id1c AS id1c_2, t0.parent_id1c AS parent_id1c_3, t0.code AS code_4, t0.name AS name_5, t0.description AS description_6, t0.description2 AS description2_7, t0.title AS title_8, t0.meta_h1 AS meta_h1_9, t0.meta_description AS meta_description_10, t0.retail_limit AS retail_limit_11, t0.limit_sales AS limit_sales_12, t0.type_of_products AS type_of_products_13, t0.show_retail_price AS show_retail_price_14, t0.ueprice AS ueprice_15, t0.status AS status_16, t0.sort AS sort_17, t0.parent_id AS parent_id_18 FROM category t0 WHERE t0.id = ? [1621] []

As I understand the situation is this:
1. The doctrine gets the product
2. After that it gets the product category
3. Gets all the products of category
A, the desired product is in item 3 and everything else.
How can this looping problem be avoided?
How can I get a product object without loading the associated category

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Skobkin, 2016-09-20
@skobkin

Firstly, your search() is useless, since such a selection can be made by the standard repository method - findBy() or the magic findById1c().
Secondly, where did you get the idea that some kind of looping is happening? The third point should not happen, unless, of course, you explicitly specified fetch="EAGER" on both sides of the relationship. And even then, Doctrine would probably not allow such a problem.

S
shagguboy, 2016-09-20
@shagguboy

write not through createQueryBuilder but a normal dql query

V
Victor Diditsky, 2016-09-20
@GhostSt92

Mdaa. I sprinkle ashes on my head. The problem lay not in doctrine, but in crooked hands and lack of knowledge.
The problem turned out to be this:
In the Category entity, a self-referenced association is configured, there are parent and children fields.
I used the search method in api and returned a serialized object.
And it turned out that when normalizing objects, the process got stuck on category-> parent-> children-> category.
It is strange that an error was not displayed, but here I have a guess. Since the children list consists of a dozen categories, the normalizer didn't recognize the loop. But here I'm not sure.
shagguboy and Alexey Skobkin I express my deep gratitude to you for your advice. Thanks to you, I started to dig in the right direction! Thank you!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question