Jimmy T.
Jimmy T.

Reputation: 4190

Doctrine: Only load ID of relationship

Is it possible with Doctrine to say that I only want to get the ID of an relationship (n:1) loaded? The ID itself is a natural value and in most times I don't need the related entity.

Upvotes: 0

Views: 694

Answers (1)

Gedrox
Gedrox

Reputation: 3612

Mark the association as lazy. It should not load the object, just a proxy. Object will be initialized immediately after any method is called, except the call for the ID.

This is the syntax – http://doctrine-orm.readthedocs.org/en/2.1/reference/annotations-reference.html#manytoone.

Example of "getId" method in the generated proxy class:

public function getId()
{
    if ($this->__isInitialized__ === false) {
        return (int) $this->_identifier["id"];
    }
    $this->__load();
    return parent::getId();
}

As you see the record won't be loaded if the proxy is not initialized.

Upvotes: 1

Related Questions