【问题标题】:Doctrine/symfony : creating 2 many-to-many relations(2 intermediate tables) on the SAME two tablesDoctrine/symfony:在相同的两个表上创建 2 个多对多关系(2 个中间表)
【发布时间】:2011-08-09 13:03:52
【问题描述】:

在我非常“罕见”的情况下,我必须在同一张表上创建 2 个多对多关系。 我解释: 我有两张桌子; Monitor 和 Server 具有多对多 rel 的中间表将被称为“基准”。但同时我必须有另一个中间表,它可以让我将一个来自监视器的 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

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

  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

帮助 ---------> 求救

【问题讨论】:

    标签: symfony1 doctrine symfony-1.4 doctrine-1.2


    【解决方案1】:

    您的Alert 模型是什么样的? (那是Url_ip吗?)

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

    Monitor:
      monitor1:
        ....
    
    Alert:
      alert_a:
        Monitor: monitor1
        Group: group1
    

    因为您现在遇到的错误意味着您添加了 Alert 并引用了不存在的监视器。当Alert 插入Monitor 之前时,可能会发生这种情况。通过键而不是 id 引用 Monitor,symfony 将首先插入 Monitor

    【讨论】:

    • 我已经修改了我的问题并添加了警报结构。请阅读它
    • 我这样做了 :alert_a: Monitor: monitor_one Groups: Group_admin
    • 但是我有这个错误:“(alert) alert_a”中提到的类应该是“Groups”并且给出了“sfGuard Group”
    • 我使用 sfGuard 自己的夹具
    【解决方案2】:

    就是这样,我发现了一个小技巧! 事实上,在监视器中,我确实像你说的那样:用过钥匙! 但在组中我正常处理,但使用 sfGuard 的 id,而不是我的!

    Alert:
      alert_a:
        Monitor: monitor1
        sf_guard_group: group1
    

    【讨论】:

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