简体   繁体   中英

Fetch entity without related objects doctrine

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM