简体   繁体   English

Doctrine2如何设置Id值

[英]How does Doctrine2 set the Id values

I have a question out of curiosity about the inner workings of Doctrine2. 我对一个关于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. 正如你会注意到有一件事显然不存在,没有办法设置id,但是学说工厂返回带有set id的实体。 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? 如何在不在类层次结构中的情况下覆盖受保护的值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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