Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 303
pacification Avatar asked Jun 15 '16 16:06

pacification


People also ask

What is persist doctrine?

The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share. You can use these interfaces and abstract classes to build your own mapper if you don't want to use the full data mappers provided by Doctrine.

What is a repository doctrine?

A repository in a term used by many ORMs (Object Relational Mappers), doctrine is just one of these. It means the place where our data can be accessed from, a repository of data.

How doctrine works?

Doctrine uses the Identity Map pattern to track objects. Whenever you fetch an object from the database, Doctrine will keep a reference to this object inside its UnitOfWork. The array holding all the entity references is two-levels deep and has the keys root entity name and id.

Are objects missing when traversing associations in doctrine?

Although Doctrine allows for a complete separation of your domain model (Entity classes) there will never be a situation where objects are missing when traversing associations. You can walk all the associations inside your entity models as deep as you want.

How does doctrine handle the public API of an entity class?

Doctrine does NEVER touch the public API of methods in your entity classes (like getters and setters) nor the constructor method. Instead, it uses reflection to get/set data from/to your entity objects. When Doctrine fetches data from DB and saves it back, any code put in your get/set methods won't be implicitly taken into account.

How to count related entities without loading them in Entity Framework?

Using Query to count related entities without loading them Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading. The techniques shown in this topic apply equally to models created with Code First and the EF Designer.

What happens when an entity is detached from an EntityManager?

An entity is detached from an EntityManager and thus no longer managed by invoking the EntityManager#detach ($entity) method on it or by cascading the detach operation to it. Changes made to the detached entity, if any (including removal of the entity), will not be synchronized to the database after the entity has been detached.


1 Answers

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.

like image 95
Richard Avatar answered Oct 15 '22 01:10

Richard