Cakephp afterFind()帮助

时间:2010-10-27 06:29:48

标签: cakephp find cakephp-1.3

我在数组中有这样的搜索结果

Array
(
    [0] => Array
        (
            [StatusMessage] => Array
                (
                    [id] => 1
                    [pid] => 0
                    [message] => First Status Message
                    [item_id] => 1
                    [commenters_item_id] => 2
                    [created] => 2010-10-26 02:09:05
                    [modified] => 2010-10-26 02:09:09
                )

            [Item] => Array
                (
                    [id] => 1
                    [module_id] => 1
                    [item_id] => 1
                    [User] => Array
                        (
                            [id] => 1
                            [first_name] => Harsha
                            [last_name] => Vantagudi
                            [username] => harshamv
                            [file] => 
                        )

                )

            [StatusMessageReply] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [pid] => 1
                            [message] => Second Status Message
                            [item_id] => 1
                            [commenters_item_id] => 1
                            [created] => 2010-10-26 02:09:05
                            [modified] => 2010-10-26 02:09:09
                            [Item] => Array
                                (
                                    [id] => 1
                                    [module_id] => 1
                                    [item_id] => 1
                                    [User] => Array
                                        (
                                            [id] => 1
                                            [first_name] => Harsha
                                            [last_name] => Vantagudi
                                            [username] => harshamv
                                            [file] => 
                                        )

                                )

                        )

                )

        )

)

我想通过删除数组中的某些东西来修改它以使它看起来更漂亮

Array
(
    [0] => Array
        (
            [StatusMessage] => Array
                (
                    [id] => 1
                    [pid] => 0
                    [message] => First Status Message
                    [item_id] => 1
                    [commenters_item_id] => 2
                    [created] => 2010-10-26 02:09:05
                    [modified] => 2010-10-26 02:09:09
                )

                    [User] => Array
                        (
                            [id] => 1
                            [first_name] => Harsha
                            [last_name] => Vantagudi
                            [username] => harshamv
                            [file] => 
                        )

            [StatusMessageReply] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [pid] => 1
                            [message] => Second Status Message
                            [item_id] => 1
                            [commenters_item_id] => 1
                            [created] => 2010-10-26 02:09:05
                            [modified] => 2010-10-26 02:09:09
                                    [User] => Array
                                        (
                                            [id] => 1
                                            [first_name] => Harsha
                                            [last_name] => Vantagudi
                                            [username] => harshamv
                                            [file] => 
                                        )

                        )

                )

        )

)

基本上删除项目部分并将用户上移一级

2 个答案:

答案 0 :(得分:4)

我会说。除了你,没有人会看到或关心这个。改变模型数据的内部格式并没有什么好处。相反,它只会引起问题。保持内部数据格式一致!

您是使用$statusMessage['Item']['User']还是$statusMessage['User']访问用户信息,实际上并没有太大的区别。如果您坚持,我建议您通过查询中的users表直接join status_messages表到items表,以放弃额外的步骤获取Item,Cake会自动按照你想要的方式格式化结果。

答案 1 :(得分:1)

如果要限制返回的关联表和/或字段,请查看Containable。如果你有大量不感兴趣的东西归还你不需要的东西,你可以通过“包含”你想要的东西来减轻处理。

但正如Deceze所说,纯粹为了美学而修剪它。