简体   繁体   English

CakePHP模型查询返回数据格式

[英]CakePHP Model Query Return Data Formating

I'm looking for a way to make it so cake returns all database data in the same format/structure... Currently it returns two different types of format depending on the relationship. 我正在寻找一种使之成为蛋糕的方法,使cake以相同的格式/结构返回所有数据库数据...目前,根据关系,它返回两种不同类型的格式。

If a model 'B' is associated with the current model 'A' being queried it will then place model associations for 'B' underneath it as you can see in [User] below. 如果模型“ B”与正在查询的当前模型“ A”相关联,则将在其下方放置“ B”的模型关联,如下面的[用户]中所示。 I want it so that all queries use that structure. 我想要它,以便所有查询都使用该结构。

example: $this->find('all', .... returns: 例如:$ this-> find('all',....返回:

Array
(
    [0] => Array
        (
            [UserGroup] => Array
                (
                    [id] => 53
                    [user_id] => 100003332014851
                    [media_id] => 
                    [name] => john
                    [description] => qwasdfad
                )


            [User] => Array
                (
                    [id] => 100003332014851
                    [session_id] => ssm2qbrotmm13ho1ipm8ii2492
                    [username] => 
                    [password] => -1
                    [Planner] => Array
                         (
                         )

                     [Purchase] => Array
                         (
                         )

                     [Listing] => Array
                         (
                         )
               )
       )

I want this to look like: 我希望它看起来像:

Array
(
    [0] => Array
        (
            [UserGroup] => Array
                (
                    [id] => 53
                    [user_id] => 100003332014851
                    [media_id] => 
                    [name] => john
                    [description] => qwasdfad
                    [User] => Array
                         (
                              [id] => 100003332014851
                              [session_id] => ssm2qbrotmm13ho1ipm8ii2492
                              [username] => 
                              [password] => -1
                              [Planner] => Array
                                   (
                                   )

                               [Purchase] => Array
                                   (
                                   )

                               [Listing] => Array
                                   (
                                   )
                            )
                     )
               )
       )

In CakePHP, the find() method return data like your first format. 在CakePHP中, find()方法返回的数据类似于您的第一种格式。 But If you want to format like second one then you have to process it by hand (try to avoid this if possible) 但是,如果您要像第二张纸一样格式化,则必须手动处理它(如果可能,请尽量避免这种情况)

$data = $this->find('all');
$assocs = Set::extract('/User', $data); // extracting all `User` array

foreach($assocs as $key => $assoc) {
    unset($data[$key]['User']); // removing the associate `User` from `$data`
    $data[$key]['UserGroup']['User'] = $assoc['User']; // adding associate under `UserGroup`
}

ended up doing this... it changes the output to what we need. 最终这样做了...它将输出更改为我们需要的。 The top level item does not have a header which is fine I just adjusted our scripts for that... maybe this will help somebody else if they need a custom idea 顶层项目没有标题,这很好,我只是为此调整了脚本……如果他们需要自定义的想法,这可能会对其他人有所帮助

also no guarantee this covers all possible results but so far it works with all the queries we have. 也不能保证它涵盖所有可能的结果,但到目前为止,它可以与我们拥有的所有查询一起使用。

class AppModel extends Model {
    function afterFind($results, $primary) {
        //if this is a primary, structure like a secondary so entire site is same format
        if ($primary) {
            $class = get_class($this);

            //simple fix for primary
            foreach ($results as $key => $result) {
                $result = $this->formatData($result, $class);
                $results[$key] = $result;
            }
        }

        return $results;
    }

    function formatData($result, $class) {
        $array = array();
        if (isset($result[$class])) {
            $array = $result[$class];
            unset($result[$class]);
        }
        $array += $result;
        return $array;
    }

在这种情况下,您还可以将contains和find作为UserGroup.User一起使用,以获得所需的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM