如何在CakePHP中从对象中删除嵌套对象


How do I remove nested object from an object in CakePHP?

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',
                            )
                        ));

非常感谢你的建议。

您可以使用其内置的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)));

我认为CakePHP不支持这一点。如果您想用一种简单的方法来完成此操作,请检查Set Utility。

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