6

I have the following classes:

class Category {

    /**
    * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
    */
    private $products;

    ...
}

class Product {

    ...

    /**
    * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
    * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
    */
    private $category;

    ...

}

when i try to fetch one product from my db like this:

    $query = $doctrineManager->createQuery(
        "
SELECT p FROM AppBundle:Product p
WHERE p.id = :id
        "
    )->setParameter('id', $id);

    $result = $query->getSingleResult();

i get not only my product, but also get category with all products (except the one i found). So, how can i fetch only model what i want without any related model?

4
  • why don't you use $this->getDoctrine()->getRepository('AppBundle:Product')->find($id)? Commented Jun 15, 2016 at 18:04
  • @DenisAlimov it doesn't matter. i get same result with what you post. Commented Jun 15, 2016 at 18:09
  • it called associations DQL SELECT statements are a very powerful way of retrieving parts of your domain model that are not accessible via associations. Additionally they allow to retrieve entities and their associations in one single SQL select statement which can make a huge difference in performance in contrast to using several queries. doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/… Commented Jun 15, 2016 at 18:22
  • 1
    @DenisAlimov i know what it is. My question was So, how can i fetch only model what i want without any related model? Commented Jun 15, 2016 at 18:25

1 Answer 1

7

They are just stubs, you don't actually fetch any related entity information unless you are using fetch=EAGER.

This answer explains it pretty well.

What is the difference between fetch="EAGER" and fetch="LAZY" in doctrine

In summary, you can't get rid of the associations, but they don't load the other entities until you call the data unless you specifically request otherwise. So don't worry about it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.