1
votes

I have a question out of curiosity about the inner workings of Doctrine2. I as a User see a really clean and robust interface, but there must be some heavy magic at work under the hood.

When I generate a simple entity it looks something like this:

class SimpleEntity
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $title;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }
}

As you will notice one thing is conspicuously absent, there is no way to set the id, but nevertheless doctrines factories return entities with set ids. How can that work? I have tried to look through the source, but lost the track somewhere.

How can it be possible to overwrite protected values without being in the class hierarchy?

1
Maybe there is something about the @ORM annotations. I think this is where the magic happens. - florian
The annotations give doctrine a entry point through reflection but, is reflection capable of setting values? This would even contradict the name... - FloydThreepwood

1 Answers