加入Propel导致递归警告

时间:2015-12-07 18:08:09

标签: php mysql recursion propel

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

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除了" remote"之外的每一列。外键。

1 个答案:

答案 0 :(得分:2)

所以似乎toJSON()创建了整个 Collection的JSON表示(包括其元数据),其中包含允许在上下移动的对象。对象层次结构,即Printer包含对Document的引用,Printer反过来将引用保存回其父级 - $printers = \PrinterQuery::create() ->joinWith("Document") ->useDocumentQuery() ->joinWith("Owner") ->endUse() ->joinWith("Recipient") ->setFormatter('\Propel\Runtime\Formatter\ArrayFormatter') ->find(); return $printers->toJSON(); } 。一旦掌握应该如何完成,这是一个漂亮的小功能。

在这种情况下,

解决方案是按照建议Formatter添加here

最终结果如下:

function selectCountry(){
    $result = @mysql_query("SELECT * from country");
    while($record = @mysql_fetch_array($result)){
        echo '<option value="'.$record['countryid'].'">'.$record['countryname'].'</option>';
    }
}