views:

41

answers:

2

i have a Search Result like this in an array

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] => 
                                        )

                                )

                        )

                )

        )

)

i wanna modify it to make it look a little prettier by removing certain things in the array

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] => 
                                        )

                        )

                )

        )

)

Bascially remove the Item part and shift the User up a level

+3  A: 

I'd say don't. Nobody is going to see or care about this except you. And there's no real benefit to changing the internal format of model data. On the contrary, it can only cause problems. Keep your internal data formats consistent!

Whether you access the user information using $statusMessage['Item']['User'] or $statusMessage['User'] really doesn't make that much of a difference. If you insist on it though, I'd suggest you join the users table directly to the status_messages table via the items table in the query to forgo the extra step of fetching the Item, and Cake will automatically format the results the way you want.

deceze
oh ok. i thought the code would get ugly and needs the refactoring :D thanks.
Harsha M V
+1  A: 

If you want to restrict the associated tables and or fields that are returned you should look at Containable. When you have a big pile of uninteresting stuff returned that you don't need, you can ease the processing a little by 'containing' it to what you want.

But as Deceze says, do not trim it purely for aesthetics.

Leo