【问题标题】:Symfony - post elements of an array to dbSymfony - 将数组的元素发布到数据库
【发布时间】:2018-06-26 12:06:54
【问题描述】:

我得到了这个文件..My list of arrays

我需要填充到 db 中的字段(类型和数据字段)。我将所有数组对象作为字符串返回的一个字段(数据)。还有另一种(类型)我需要返回卡授权、卡财务等。

这是我的数据库。

My database

我的函数正在填充数据字段,但我不知道如何填充我提到的类型字段。

这是我的代码..

public function serializeTransactions()
{
    $xml = simplexml_load_file("/var/www/html/web/uploads/gps-trans.xml");

    $response = json_decode(json_encode((array)$xml), true);

    foreach ($xml as $result) {
        $result = (array)$result;
        $gps = new Transactions();
        $gps->setType();
        $gps->setData(json_encode($result));

        $this->em->persist($gps);
    };

    $this->em->flush();
}

对不起,如果我的英语不是那么好,我真的需要一些帮助。

【问题讨论】:

  • 很抱歉,您的问题是什么?您的代码是否无法正常工作?是怎么回事?
  • $gps->setType();我需要用卡授权、卡财务等填充该字段。 (见图).. 其他字段工作正常。

标签: php arrays database symfony


【解决方案1】:

你的代码很奇怪……

$response = json_decode(json_encode((array)$xml), true);

是为了什么?

但是...如果您想使用$gps->setType(); 保存类型并假设您的 $result 是 SimpleXMLElement 的类型...那么

foreach ($xml as $result) {
    $gps = new Transactions();
    $gps->setType($result->getName());
    $gps->setData(json_encode((array)$result));

    $this->em->persist($gps);
};

???

【讨论】:

  • 调用数组上的成员函数 getName() 是错误的。如果我尝试 $result->getName($xml) 它会返回整个 xml 文件的名称,而我需要的是 Card Authorisation、Card Financial 等。
  • 编辑了我的答案...只需将类型转换从 SimplexXmlElement 删除到数组,然后在需要时添加它...如果您不在正确的深度,只需深入 xml 树...我不知道你的初始 xml
【解决方案2】:

感谢大家重播。我找到了解决方案。

 public function serializeTransactions()
{
    $xml = simplexml_load_file("/var/www/html/web/uploads/gps-trans.xml");

    foreach ($xml as $key => $result) {

        $result = (array)$result;
        $gps = new Transactions();
        $gps->setType($key);
        $gps->setData(json_encode($result));

        $this->em->persist($gps);
    };

    $this->em->flush();
}

【讨论】:

    猜你喜欢
    • 2023-02-16
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2018-01-02
    • 2017-01-24
    • 1970-01-01
    • 2017-03-30
    • 2011-12-24
    相关资源
    最近更新 更多