Doctrine / symfony:在SAME两个表上创建2个多对多关系(2个中间表)

时间:2011-08-09 13:03:52

标签: symfony1 doctrine symfony-1.4 doctrine-1.2

在我非常“罕见”的情况下,我必须在同一个表上创建2个多对多关系。 我解释: 我有两张桌子;具有多对多rel的Monitor和Server将称为“Benchmark”。但同时我必须有另一个中间表,可以让我从监视器将一个URL连接到服务器的几个ip(该表称为“Url_ip”) 所以这就是我所做的:

    Monitor:
      tableName: monitor
      actAs:
        Timestampable: ~
      columns:
        id : {type: integer(4), primary: true, autoincrement: true}
        label: {type: string(45)}
        url: {type: string(80)}
        frequency: {type: integer}
        timeout: {type: integer}
        method: {type: enum, values: [GET, POST]}
        parameters: {type: string(255)}
      relations:
        Groups:
          class: Groups
          local: monitor_id
          foreign: sf_guard_group_id
          refClass: Alert
        Server:
          class: Server
          local: monitor_id
          foreign: server_id
          refClass: Benchmark
        Server2:
          class: Server
          local: monitor_id
          foreign: server_id
          foreignAlias: Ips
          refClass: Url_ip

    Url_ip:
      actAs:
        Timestampable: ~
      columns:
        monitor_id: { type: integer(4), primary: true }
        server_id: { type: integer(4), primary: true }
      relations:
        Monitor:
          foreignAlias: IpUrls
        Server:
          foreignAlias: IpUrls       

    Benchmark:
      tableName: benchmark
      actAs:
        Timestampable: ~
      columns:
        monitor_id: { type: integer(4), primary: true }
        server_id: { type: integer(4), primary: true }
        connexionTime: {type: string(45)}
        executionTime: {type: string(45)}
        responseTime: {type: string(45)}
        responseCode: {type: string(45)}
        responseMessage: {type: string(45)}
      relations:
        Monitor:
          foreignAlias: ServerMonitors
        Server:
          foreignAlias: ServerMonitors



    Server:
      tableName: server
      actAs:
        TimeStampable: ~
      columns:
        id : {type: integer(4), primary: true,autoincrement: true}
        name: {type: string(255)}
        ip: {type: string(45)}
      relations:
        Monitor:
          class: Monitor
          local: server_id
          foreign: monitor_id
          refClass: Benchmark
        Monitor2:
          class: Monitor
          foreignAlias: monitors
          local: server_id
          foreign: monitor_id
          refClass: Url_ip


Alert:
  actAs:
    Timestampable: ~
  columns:
    monitor_id: { type: integer(4), primary: true }
    sf_guard_group_id: { type: integer, primary: true }
  relations:
    Monitor:
      foreignAlias: GroupMonitors
    sfGuardGroup:
      foreignAlias: GroupMonitors

实际上这似乎有效,因为doctrine可以创建相应的表。 问题是加载修复程序时。我有这个错误:

  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a c
hild row: a foreign key constraint fails (`sfmonitoring`.`alert`, CONSTRAINT `al
ert_monitor_id_monitor_id` FOREIGN KEY (`monitor_id`) REFERENCES `monitor` (`id`
))

fixures / monitors.yml

Monitor:
  monitor_one:
    id: 1
    label: task1
    url: www.task1.com
    frequency: 5
    timeout: 30
    method: GET
    parameters: a=1&b=2

  monitor_two:
    id: 2
    label: task2
    url: www.task2.com
    frequency: 5
    timeout: 20
    method: POST
    parameters: a=11&b=22

  monitor_three:
    id: 3
    label: task3
    url: www.task3.com
    frequency: 10
    timeout: 30
    method: GET
    parameters: a=111&b=211

fixures / benchmark.yml

Benchmark: 
      bench_one:
        monitor_id: 1
        server_id: 1
        connexionTime: 25
        executionTime: 25
        responseTime: 25
        responseCode: 200
        responseMessage: message de réponse

      bench_two:
        monitor_id: 2
        server_id: 2
        connexionTime: 25
        executionTime: 25
        responseTime: 25
        responseCode: 200
        responseMessage: message de réponse

      bench_three:
        monitor_id: 3
        server_id: 3
        connexionTime: 25
        executionTime: 25
        responseTime: 25
        responseCode: 200
        responseMessage: message de réponse

      bench_Four:
        monitor_id: 1
        server_id: 2
        connexionTime: 25
        executionTime: 25
        responseTime: 25
        responseCode: 200
        responseMessage: message de réponse

fixures / alerts.yml

Alert:
  alert_a:
    monitor_id: 1
    sf_guard_group_id: 1

  alert_b:
    monitor_id: 2
    sf_guard_group_id: 2Alert:
  alert_a:
    monitor_id: 1
    sf_guard_group_id: 1

  alert_b:
    monitor_id: 2
    sf_guard_group_id: 2

帮助---------> S.O.S

2 个答案:

答案 0 :(得分:0)

您的Alert型号是什么样的? (这是Url_ip?)

最好通过密钥而不是ID来引用模型。

Monitor:
  monitor1:
    ....

Alert:
  alert_a:
    Monitor: monitor1
    Group: group1

由于您现在收到的错误,表示您添加Alert并引用了不存在的监视器。在Alert之前插入Monitor时可能会发生这种情况。通过按键而不是id引用Monitor,symfony将首先插入Monitor

答案 1 :(得分:0)

就是这样,我发现了一个小技巧!  事实上在显示器中,我确实喜欢你说:用过钥匙! 但在集团我正常处理,但使用sfGuard的id,而不是我的!

Alert:
  alert_a:
    Monitor: monitor1
    sf_guard_group: group1