Search code examples
phpjsonpropel

Converting Propel to JSON with linked entities


I have this setup in a PHP project using Propel (which I'm new to).

User: userid, firstname, lastname

SignIn: signinid, userid, time

SignIn, in this case, is a table containing the times each user signed in.

What I want is to print out a JSON string using Propel of the last ten SignIn entries. On the front end I want to display something like:

Bob Builder signed in at 3:30pm
Max Power signed in at 3:24pm
...

So when I query the last ten entries in SignIn and then call toJSON, I'd like to have the User data for that SignIn record also included in the JSON.

How can I do that?

Note, if there's a better way to do this, I'm open to ideas.


Solution

  • One thing about Propel, is that the documentation is really clean, very readable and always helpful.

    For your request, you can try the following request. Take a closer look at it, it's readable (as always with Propel)

    $signIn = SignInQuery::create()
      ->select(array('User.firstname', 'User.lastname', 'SignIn.time'))
      ->join('User')
      ->orderByTime('desc')
      ->limit(10)
      ->find()
      ->toJson();