1
votes

I was wondering whether the following would be possible using Doctrine 2.4.

Suppose we have an entity that looks like this:

/**
 * @ORM\Entity
 * @ORM\Table
 */
class SomeEntity {
    /**
     * @ORM\Id
     * @ORM\Column(type="bigint")
     * @ORM\GeneratedValue
     */
    protected $some_really_long_named_id;
    public function getSomeReallyLongNamedId() { return $this->some_really_long_named_id; }
}

Since we work in IT and we're lazy, I added this method:

public function getId() { return $this->some_really_long_named_id; }

When this entity is proxied and we call getId() on the proxy of this entity, the entity is lazy loaded. Which is weird, as that doesn't happen when we call getSomeReallyLongNamedId().

Apparently this has to do with some magic inside Doctrine's proxy objects. The generated proxy methods look like this:

    /**
     * {@inheritDoc}
     */
    public function getId()
    {

        $this->__initializer__ && $this->__initializer__->__invoke($this, 'getId', array());

        return parent::getId();
    }

    /**
     * {@inheritDoc}
     */
    public function getSomeReallyLongNamedId()
    {
        if ($this->__isInitialized__ === false) {
            return  parent::getSomeReallyLongNamedId();
        }


        $this->__initializer__ && $this->__initializer__->__invoke($this, 'getSomeReallyLongNamedId', array());

        return parent::getSomeReallyLongNamedId();
    }

Is there any way to influence the ProxyGenerator to make sure that when the getId() method is called, it doesn't imply a lazy-load?

1

1 Answers

0
votes

I know your question i quite old, but i'm searching a solution to the same problem.

I found a way to do disable the direct lazy-load with annotations :

/** @Id **/
protected $id;

/** @IdGetter **/
public function getId() { 
 ...
}

that will force a initialization test in the proxy class :

/**
 * {@inheritDoc}
 */
public function getId()
{
    if ($this->__isInitialized__ === false) {
       return  parent::getId();
    }

    $this->__initializer__ && $this->__initializer__->__invoke($this, 'getId', array());

    return parent::getId();
}

EDIT #1

The getId() method MUST be less than 4 lines (@see in ProxyGenerator.php method isShortIdentifierGetter()) to be detected as an identity getter.