Doctrine2如何设置Id值 [英] How does Doctrine2 set the Id values

查看:146
本文介绍了Doctrine2如何设置Id值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Doctrine2的内在工作,我有一个好奇的问题。作为用户,我看到一个非常干净和健壮的界面,但是在工作中必须有一些重的魔力。



当我生成一个简单的实体时,它看起来像这个:

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

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

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

/ **
*设置标题
*
* @param string $ title
* /
public function setTitle($ title)
{
$ this-> title = $ title;
}

/ **
*获取标题
*
* @return string
* /
public function getTitle )
{
return $ this-> title;
}
}

正如你会注意到一件事显然不存在,那里没有办法设置id,但是理论工具返回具有set id的实体。该怎么办?我试图查看源代码,但在某处丢失了轨道。



如何覆盖受保护的值而不在类层次结构中?

解决方案

答案是反射。请参阅 http://www.doctrine-project.org/docs/orm/2.1/en/tutorials/getting-started-xml-edition.html#a-first-prototype



没有挖掘原则来源,我会说它使用 ReflectionProperty :: setValue()


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 answer is Reflection. See http://www.doctrine-project.org/docs/orm/2.1/en/tutorials/getting-started-xml-edition.html#a-first-prototype

Without digging into the Doctrine source, I'd say it makes use of ReflectionProperty::setValue()

这篇关于Doctrine2如何设置Id值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆