简体   繁体   中英

How does Doctrine2 set the Id values

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM