Doctrine Fixtures中的多个引用

时间:2010-06-16 19:25:21

标签: php doctrine fixtures

我有以下模特:

class User extends Doctrine_Record {
    public function setTableDefinition() {
        $this->hasColumn ( 'username', 'string', 20 );
        $this->hasColumn ( 'password', 'string', 40 );
        $this->hasColumn ( 'salt', 'string', 40 );
        $this->hasColumn ( 'email', 'string', 80 );
    }

    public function setUp() {
        $this->setTableName ( 'users' );

        $this->hasMany ('Message as SentMessages', array(
            'local' => 'id',
            'foreign' => 'sender_id'
        ));
        $this->hasMany ('Message as ReceivedMessages', array(
            'local' => 'id',
            'foreign' => 'recipient_id'
        ));
    }
}


class Message extends Doctrine_Record {
    public function setTableDefinition() {
        $this->hasColumn ( 'sender_id', 'integer', 4,
                array(  'notnull'=> true,
                        'unsigned'=>true
        ));
        $this->hasColumn ( 'recipient_id', 'integer', 4,
                array(  'notnull'=> true,
                        'unsigned'=>true
        ));
        $this->hasColumn ( 'title', 'string', 20 );
        $this->hasColumn ( 'content', 'string',1000);
    }

    public function setUp() {
        $this->setTableName ( 'messages' );

        $this->hasOne ('User', array(
            'local' => 'sender_id',
            'foreign' => 'id'
            ));
        $this->hasOne ('User as Recipient', array(
            'local' => 'recipient_id',
            'foreign' => 'id'
            ));
    }
}

我需要Fixtures自动加载我的测试环境

User:
  FooUser:
    username: FooUser
    password: foobar
    email: foobar@example.com
  TestUser:
    username: Testuser
    password: foobar
    email: foobar2@example.com
Message:
  Message1:
    User: FooUser
    User: TestUser
    title: Testmessage 1
    content: This is message 1
  Message2:
    User: TestUser
    User: FooUser
    title: Testmessage 2
    content: This is message 2

这不起作用,因为我不能与灯具中的同一个表有2个关系。 有没有解决这个问题?

1 个答案:

答案 0 :(得分:1)

在灯具中使用别名,而不是型号名称:

Message:
  Message1:
    User: FooUser
    Recipient: TestUser
    title: Testmessage 1
    content: This is message 1
  Message2:
    User: TestUser
    Recipient: FooUser
    title: Testmessage 2
    content: This is message 2