【问题标题】:How do I remove nested object from an object in CakePHP?如何从 CakePHP 中的对象中删除嵌套对象?
【发布时间】:2014-10-20 20:23:12
【问题描述】:

CakePHP API 返回结果如下:

{
    "status": "OK",
    "themes": [
        {
            "Theme": {
                "id": "20",
                "user_id": "50",
                "name": "dwdwdw",
                "language_code_from": "cz",
                "language_code_to": "en",
                "type": "CUSTOM",
                "created": "2014-10-19 15:36:05",
                "count_of_cards": 0
            }
        }
    ]
}

我想问一下,如何删除嵌套的主题对象得到这样的结果?:

{
    "status": "OK",
    "themes": [
        {
                "id": "20",
                "user_id": "50",
                "name": "dwdwdw",
                "language_code_from": "cz",
                "language_code_to": "en",
                "type": "CUSTOM",
                "created": "2014-10-19 15:36:05",
                "count_of_cards": 0
        }
    ]
}

这是我的 CakePHP 代码:

$this->Theme->recursive = -1;
                        // GET USER ID
                        $themeData['user_id'] = $isSessionValid;
                        // GET ALL THEMES RELATED TO USER
                        $foundThemes = $this->Theme->find('all', array(
                                'conditions' => array(
                                    'Theme.user_id' => $themeData['user_id'])
                            )
                        );
                        $themes = array();
                        // FOREACH THEMES AND GET COUNT FOR CARDS FOR EACH THEME
                        foreach($foundThemes as $foundTheme) {
                            // GET COUNT OF QUESTIONS FOR ACTUAL THEME
                            $countOfCards = $this->Theme->Card->find('count', array(
                                'conditions' => array(
                                    'Card.theme_id' => $foundTheme['Theme']['id'])
                                )
                            );
                            // APPEND TO ACTUAL ARRAY
                            $foundTheme['Theme']['count_of_cards'] = $countOfCards;
                            array_push($themes,$foundTheme);
                        }

                        // SET SUCCESS RESPOSNSE
                        $this->set(array(
                            'status' => 'OK',
                            'themes' => $themes,
                            '_serialize' => array(
                                'status',
                                'themes',
                            )
                        ));

非常感谢您的建议。

【问题讨论】:

  • 您是说您不喜欢set 的值正在返回您的视图吗?如果是这样 - 您是否尝试将 themes 键更改为 'themes' => $themes['Theme']

标签: php json cakephp object nested


【解决方案1】:

您可以使用 CakePHP 内置的 Hash 实用程序来操作 CakePHP 的数组格式:http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash

我会做的是使结果变平:

$results = Hash::flatten($results);

您的数据数组最终会变成一个如下所示的一维数组:

$results = array(
    'status' => 'OK'
     'themes.0.Theme.id' => 20,
     ...
     'themes.1.Theme.id' => 21,
     ...
);

然后您可以使用字符串替换从您的键中删除“主题”:

$keys = array_keys($results);
$keys = str_replace('Theme.', '', $keys);

然后你可以使用 Hash::expand 来获取你的原始数组,现在可以按照你想要的方式格式化:

$results = Hash::expand(array_combine($keys, array_values($results)));

【讨论】:

    【解决方案2】:

    我不认为 CakePHP 支持这个。如果您想以简单的方式执行此操作,请检查设置实用程序。

    http://book.cakephp.org/2.0/en/core-utility-libraries/set.html

    【讨论】:

    • 你能提供一些例子吗?有很多set函数用法。
    • $foundThemes = Set::classicExtract($tasks, '{n}.Theme'); 这就是你需要的;
    猜你喜欢
    • 2021-12-28
    • 2020-05-27
    • 2020-12-07
    • 1970-01-01
    • 2021-01-30
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多