dptj13337 2010-06-16 19:25
浏览 8
已采纳

Doctrine Fixtures中的多个引用

I have following models:

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

And I need Fixtures to auto-load my testing environment

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

This doesn't work, because I can't have 2 relationships to the same table in the fixtures. Is there a fix for this?

  • 写回答

1条回答 默认 最新

  • doulao3078 2010-06-17 11:11
    关注

    Use the aliased name in your fixtures rather than the model name:

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站