tags:

views:

20

answers:

1

I have entity like this:

/**
 *
 * @Table(name="table")
 * @Entity
 */
 class Table {

    /**
     * @Column(type="integer")
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
     private $id;


    /**
     * @ManyToOne(targetEntity="Entities\Users")
     * @joinColumn(name="userId", referencedColumnName="id")
     */
     private $User;


    /**
     * @Column(type="string")
     */
     private $text;


}

If i do $q->getQuery()->getSingleResult()->getUser()->getUserId()

doctrine generate query like:

SELECT * FROM table t INNER JOIN users u ON u.id = t.userId WHERE id = 100

but if i don`t need table users, how to get an userId.

In pure SQL i can just

SELECT * FROM table WHERE id = 100

and get userId without join users table.

A: 

I don't know doctrine2 but from what I read this is a ORM. So I would suggest that you need a form of lazy loading where user isn't loaded. This article suggests that lazy loading is available in doctrine2.

This may result in multiple db calls if user is later used on the returned table. You have to weigh this up with the idea of grabbing the data in one hit.

Preet Sangha