开发者

Problems with behaviors in Doctrine

开发者 https://www.devze.com 2022-12-13 23:40 出处:网络
I开发者_JAVA百科 have a problem in one of my projects, where I\'m using Doctrine as an ORM. For some reason, when rebuilding the models and database structure, Doctrine ignores the behaviors and rela

I开发者_JAVA百科 have a problem in one of my projects, where I'm using Doctrine as an ORM.

For some reason, when rebuilding the models and database structure, Doctrine ignores the behaviors and relations, I define in one of the table definitions. The YAML table definition looks like this:

...

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

...

The generated table structure looks like this:

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;

As you can see, Doctrine generates all the columns I define, but for some reason all the stuff that is supposed to happen automatically is not done. First of all, it doesn't create the updated_at and created_at columns for the Timestampable behavior and the slug column for Sluggable behavior is also missing.

The indexes and foreign key constraints are also missing.

When I open up the generated model class, it looks all fine:

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

    ....

}

So, the problem lies in the SQL query generation...

Has anyone else experienced a similar problem, or can you spot any errors in my YAML definition?


Based off what you posted, and the subsequent comments, it appears there is name collision with your User table, and the mysql.user table. As you mentioned when you renamed your table to Person, it worked as expected.

In my experience, I always add an arbitrary table prefix to my tables to avoid these types of unexpected behaviors.

0

精彩评论

暂无评论...
验证码 换一张
取 消