Welcome Guest, Not a member yet? Register   Sign In
Returning standard objects with different queries
#1

[eluser]bapobap[/eluser]
Hi there,

So I have my models returning nice objects formatted in a particular way. Each MySQL query does not directly map to an object, so I basically get the query result, build the object and then return it. This works great for something like:

Code:
$this->User_model->user(123456)

which would return:

Code:
stdClass Object
(
    [id] => 2
    [username] => Peas
    [profile] => stdClass Object
        (
            [name] => Peas
            [email] => [email protected]
            [avatar] => 1
            [likes] => Green
            [dislikes] => Blue
        )
    [created] => 2009-01-01 00:00:00
    [updated] => 2009-01-01 00:00:00
)

It doesn't work well for other uses. Say I'm getting a list of users who have uploaded an image. I get a result from the image table in MySQL. It has a user_id column for me to JOIN and get the user record. If I were to use
Code:
$this->User_model->user($image->user_id)
for each image record, I'd be running an extra SQL query I didn't need, just to get my nicely formatted object back. What I'd prefer is for the user details I ask for in the image query to be formatted as it would be in the user model. To do this I need to copy and paste how the user object is built from the user model and apply it to this image result. So I'd get:

Code:
stdClass Object
(
    [id] => 2
    [width] => 320
    [height] => 240
        [user] => stdClass Object
             (
                [username] => Peas
                [profile] => stdClass Object
                    (
                        [name] => Peas
                        [email] => [email protected]
                        [avatar] => 1
                        [likes] => Green
                        [dislikes] => Blue
                    )
         [created] => 2009-01-01 00:00:00
         [updated] => 2009-01-01 00:00:00
             )

)

The fact I'm copying and pasting this for every sort of query I do that involves a user is a red flag. I know this isn't what I should be doing but I'm not sure what the solution is?


Messages In This Thread
Returning standard objects with different queries - by El Forum - 10-31-2009, 06:18 AM
Returning standard objects with different queries - by El Forum - 10-31-2009, 08:30 AM
Returning standard objects with different queries - by El Forum - 10-31-2009, 09:05 AM
Returning standard objects with different queries - by El Forum - 10-31-2009, 09:31 AM



Theme © iAndrew 2016 - Forum software by © MyBB