【问题标题】:How does Doctrine2 set the Id valuesDoctrine2 如何设置 Id 值
【发布时间】:2011-11-03 23:58:23
【问题描述】:

出于对 Doctrine2 内部工作原理的好奇,我有一个问题。作为用户,我看到了一个非常干净和强大的界面,但在后台一定有一些强大的魔法在起作用。

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

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;
    }
}

您会注意到一件事明显不存在,即无法设置 id,但原则工厂返回具有设置 id 的实体。这怎么能行?我试图查看源代码,但在某处丢失了踪迹。

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

【问题讨论】:

  • 也许有一些关于@ORM 注释的东西。我认为这就是奇迹发生的地方。
  • 注释通过反射为学说提供了一个入口点,但是反射是否能够设置值?这甚至会与名称相矛盾...

标签: php doctrine-orm


【解决方案1】:

【讨论】:

  • @Phil - 我不会这么说,可能只是缺乏熟悉。
  • @Phil 您定义了一个具有受保护/私有属性的类,并且该语言实际上为您提供了从外部访问和更改它们的方法......这些功能往往会受到太多开发人员的青睐。跨度>
  • @FloydThreepwood 在这里阅读更多内容 - en.wikipedia.org/wiki/Reflection_(computer_programming)
猜你喜欢
  • 2012-07-17
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 2012-08-31
  • 2012-09-28
  • 2017-03-25
相关资源
最近更新 更多