2

我在我的一个项目中遇到问题,我使用 Doctrine 作为 ORM。

由于某种原因,在重建模型和数据库结构时,Doctrine 忽略了行为和关系,我在其中一个表定义中定义。YAML 表定义如下所示:

...

User:  
  actAs:
    Timestampable:
    Sluggable:
      unique: true
      fields: username
      canUpdate: true
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    company_id
      type: integer(4)
    timezone_id:
      type: integer(1)
    role_id:
      type: integer(1)
    email:
      type: string(255)
    username:
      type: string(255)
      unique: true
    password:
      type: string(40)
    firstname:
      type: string(255)
    lastname:
      type: string(255)
    last_login:
      type: datetime
  relations:
    Company:
      local: company_id
      foreign: id
    Timezone:
      local: timezone_id
      foreign: id
    Role:
      local: role_id
      foreign: id

...

生成的表结构如下所示:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `company_id` int(11) DEFAULT NULL,
  `timezone_id` tinyint(4) DEFAULT NULL,
  `role_id` tinyint(4) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `username` varchar(255) DEFAULT NULL,
  `password` varchar(40) DEFAULT NULL,
  `firstname` varchar(255) DEFAULT NULL,
  `lastname` varchar(255) DEFAULT NULL,
  `last_login` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

如您所见,Doctrine 生成了我定义的所有列,但由于某种原因,所有应该自动发生的事情都没有完成。首先,它不会为 Timestampable 行为创建updated_atand列,并且Sluggable 行为的列也丢失了。created_atslug

索引和外键约束也丢失了。

当我打开生成的模型类时,它看起来一切正常:

class BaseUser extends Doctrine_Record
{

    ....

    public function setUp()
    {
        parent::setUp();
        $this->hasOne('Company', array(
             'local' => 'company_id',
             'foreign' => 'id'));

        $this->hasOne('Timezone', array(
             'local' => 'timezone_id',
             'foreign' => 'id'));

        $this->hasOne('Role', array(
             'local' => 'role_id',
             'foreign' => 'id'));

        $timestampable0 = new Doctrine_Template_Timestampable();
        $sluggable0 = new Doctrine_Template_Sluggable(array(
             'unique' => true,
             'fields' => 'username',
             'canUpdate' => true,
        ));
        $this->actAs($timestampable0);
        $this->actAs($sluggable0);
    }

    ....

}

所以,问题在于 SQL 查询的生成......

有没有其他人遇到过类似的问题,或者你能在我的 YAML 定义中发现任何错误吗?

4

1 回答 1

0

User根据您发布的内容以及随后的评论,您的表格似乎存在名称冲突,并且mysql. user桌子。正如您在将表重命名为 时提到的Person,它按预期工作。

根据我的经验,我总是在我的表中添加任意表前缀以避免这些类型的意外行为。

于 2012-03-04T22:44:04.067 回答