T
T
Timur Mukhtarov2015-04-08 11:18:02
symfony
Timur Mukhtarov, 2015-04-08 11:18:02

How to remove redundant information from $entity returned by Doctrine2, Symfony?

I have a Product entity which is related to another entity by a relationship

/**
* @ORM\ManyToOne(targetEntity="\бла\бла\Entity\Brand")
*/
protected $brand;

The essence of a brand has many properties. So when querying a product like this:
$entities = $entityManager->getRepository('бла бла\Product')->findBy(
                $filters,
                $orderBy,
                $limit,
                $offset
            );

I get the following result:
{
"id": 178,
    "sku": "ART9870",
    "barcode": "66075392",
    "supply_price": {
      
    }
    "sell_price": {
     
    }
    "properties": {
      
    }
    "company": {
     
    }
    "brand": {
      "id": 632,
      "name": "Waelchi, Will and Reinger",
      "property_dictionaries": [
        {
          "name": "velit",
          "value_list": [
            "error",
            "enim",
            "aliquid",
            "enim",
            "suscipit",
            "inventore",
            "minima",
            "aperiam",
            "quia",
            "officiis"
          ]
          "position": 0,
          "id": 1252
        }
        {
          "name": "et",
          "value_list": [
            "molestiae",
            "saepe",
            "provident",
            "voluptas",
            "numquam",
            "et",
            "harum",
            "nemo",
            "ut",
            "facilis"
          ]
          "position": 1,
          "id": 1254
        }
       
      ]
      "company": {
        "id": 316,
        "name": "Kilback, Gislason and Swift"
      }
    }
  }

So the question itself is how to make it so that instead of the entire Brand entity, only Brand id is returned

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey, 2015-04-08
@fearintino

depending on how you serialize the data. In any case, you can always just go around everything through map in some method. in combination with lazy data loading, you can do this even without much overhead

A
Alexey Pavlov, 2015-04-08
@lexxpavlov

Look at Partial objects .
Or, if your task allows, use DBAL instead of ORM - only the requested data will be returned to you in the form of an array, without associated objects (well, unless you explicitly specify them in SQL). In general, DBAL is the ability to simply execute any SQL query, without being tied to entities.

O
OnYourLips, 2015-04-08
@OnYourLips

So when querying a product like this:

Between these stages there is something that you have kept silent about. That's why you get it.
Doctrine uses lazy load by default and does not preload associated models.

D
Dima, 2015-05-22
@lucchese

You just do a dump here it is and loads everything

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question