【问题标题】:Join in Propel causes recursion warning加入 Propel 导致递归警告
【发布时间】:2015-12-07 18:08:08
【问题描述】:

我们有一个如下所示的数据库模型:

CREATE TABLE `Recipient` (
  `username` VARCHAR(15),
  `full_name` VARCHAR(45) NOT NULL,
  `email` VARCHAR(50) NOT NULL,
  `phone` VARCHAR(20) NOT NULL,
    `status` BIT NOT NULL,
  PRIMARY KEY (`username`)
) DEFAULT CHARSET=utf8;

CREATE TABLE `Printer` (
  `id` INT(6) UNSIGNED AUTO_INCREMENT,
    `arrival_date` DATETIME NOT NULL,
    `archived_date` DATETIME,
    `recipient_id` VARCHAR(15) NOT NULL,
    FOREIGN KEY(`recipient_id`) REFERENCES Recipient(`username`),
    PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8; 

CREATE TABLE `Owner` (
  `id` INT(6) UNSIGNED AUTO_INCREMENT,
  `name` VARCHAR(30) UNIQUE NOT NULL,
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8; 

CREATE TABLE `Document` (
  `serial` VARCHAR(50),
  `picture` TEXT,
    `owner_id` int(6) UNSIGNED NOT NULL,
    `printer_id` INT(6) UNSIGNED,
    FOREIGN KEY(`owner_id`) REFERENCES Owner(`id`),
    FOREIGN KEY(`printer_id`) REFERENCES Printer(`id`),
    PRIMARY KEY(`serial`)
) DEFAULT CHARSET=utf8; 

当我们调用我们的方法get_printers,它看起来像这样:

public function get_printers(){
    $printers = \PrinterQuery::create()
            ->joinWith("Document")
            ->useDocumentQuery()
            ->joinWith("Owner")
            ->endUse()
            ->joinWith("Recipient")
            ->find();
    return $printers->toJSON();
}

我们得到这个作为回应

{
  "Printers": [
    {
      "Id": 1,
      "ArrivalDate": null,
      "ArchivedDate": null,
      "RecipientId": "myusername",
      "Recipient": {
        "Username": "myusername",
        "FullName": "Sture Testsson",
        "Email": "email@example.com",
        "Phone": "07383918",
        "Status": "\u0001",
        "Printers": [
          "*RECURSION*"
        ]
      },
      "Documents": [
        {
          "Serial": "111",
          "Picture": "url",
          "OwnerId": 1,
          "PrinterId": 1,
          "Printer": "*RECURSION*"
        },
        {
          "Serial": "222",
          "Picture": null,
          "OwnerId": 2,
          "PrinterId": 1,
          "Printer": "*RECURSION*"
        },
        {
          "Serial": "333",
          "Picture": null,
          "OwnerId": 3,
          "PrinterId": 1,
          "Printer": "*RECURSION*"
        }
      ]
    }
  ]
}

问题: 是什么导致"Printer": "*RECURSION*" 发生,我们如何从响应中删除它?最好不必SELECT 除“远程”外键之外的每一列。

【问题讨论】:

  • 能否覆盖toJSON 方法并取消设置打印机字段?

标签: php mysql recursion propel


【解决方案1】:

因此,toJSON() 似乎创建了 整个 Collection(包括其元数据)的 JSON 表示,其中包含允许在对象层次结构中上下移动的对象,即Printer 拥有对 Document 的引用,而 Document 又拥有对其父级的引用 - Printer。一个漂亮的小功能,一旦你掌握了它应该如何完成。


解决方案在这种情况下是添加Formatter,建议here

最终结果如下所示:

$printers = \PrinterQuery::create()
            ->joinWith("Document")
            ->useDocumentQuery()
            ->joinWith("Owner")
            ->endUse()
            ->joinWith("Recipient")
            ->setFormatter('\Propel\Runtime\Formatter\ArrayFormatter')
            ->find();
    return $printers->toJSON();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    相关资源
    最近更新 更多