【问题标题】:Multiple References in Doctrine FixturesDoctrine Fixtures 中的多个引用
【发布时间】:2010-06-16 19:25:20
【问题描述】:

我有以下型号:

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 个关系。 有解决办法吗?

【问题讨论】:

    标签: php doctrine fixtures


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多