ZF2 - 更新实体中的字段

时间:2014-05-03 16:28:04

标签: orm doctrine zend-framework2

我目前有以下数据库结构:

table: user
fields: user_id, name, id

我从教程中获得的以下实体非常有效:

<?php
    namespace Administration\Entity;
    use Doctrine\ORM\Mapping as ORM;

    /** @ORM\Entity */

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

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

        public function setName($name) {
            $this->name = $name;
        }

        public function getName() {
            return $this->name;
        }
    }

然而问题是我想使用字段:user_id而不是id,因此将我的实体更新为以下内容以使用新字段名称:

<?php
    namespace Administration\Entity;
    use Doctrine\ORM\Mapping as ORM;

    /** @ORM\Entity */

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

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

        public function setName($name) {
            $this->name = $name;
        }

        public function getName() {
            return $this->name;
        }
    }

现在出现以下错误:

 Uncaught exception 'Doctrine\Common\Annotations\AnnotationException' with message '[Semantical Error] The annotation "@Doctrine\ORM\Mapping\user_id" in property Administration\Entity\User::$user_id does not exist, or could not be auto-loaded.' in /trunk/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php on line 52

显然错误是说user_id属性不存在,但我不明白为什么会这样。

当我删除该字段并重新添加时,我收到相同的错误

如果我将id添加为主要字段而将user_id添加为实体中的另一个字段,则不会引发错误。

是否可能所有主要字段都应该被称为id而ZF2可能需要这个?

1 个答案:

答案 0 :(得分:0)

我想出来了,基本上@ORM \ Id识别主键而不是字段名称:id ...

这有点令人困惑。