We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49407
    • 159 Posts
    I'm building a social stream or feed of user posts. So, I've created a table, "streams", that holds the "owner_id" field and the "following" field of IDs the users that they follow.

    I would like to use the array of "following" IDs to generate a collection that is sorted by DESC creation timestamp.

    How would I go about doing this?

    This question has been answered by aaronkent. See the first response.

      • 3749
      • 24,544 Posts
      Have you set up your custom table for use with xPDO?

      I sounds like the 'following' field contains multiple user IDs. Is that true, and if so how are they stored?

      When and how is the creation timestamp stored?
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 49407
        • 159 Posts
        Hi BobRay!

        Yes, I have all the tables mapped.

        The following field is a string comma separated. I'm using explode() to put the values into an array.

        The "created" field is a mySQL timestamp.

        As always I appreciate the help!
          • 3749
          • 24,544 Posts
          What's the name of your object (The one stored in the table rows)?
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 49407
            • 159 Posts
            You mean the class name? UserPosts
              • 3749
              • 24,544 Posts
              Try this (untested):

              $output = '';
              
              $query = $modx->newQuery('UserPosts');
              $query->sortby('created','DESC');
              
              $results = $xpdo->getCollection('UserPosts', $query);
              
              if (empty($results)) {
                  return 'No Data';
              }
              
              foreach ($results as $result) {
                 $fields = $result->toArray();
                 /* Create output here -- use this first to make sure it's working */
                 $output .= '<br>' . $fields['created'] . ' -- ' . $fields['following'];
              }
              
              
              return $output;
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 49407
                • 159 Posts
                I think you misunderstood, Bob.

                I want to use the array "following" as a property value to get the collection of rows from UserPosts.

                Can I pass an array as a property value like...

                $query = $modx->newQuery( 'UserPosts' );
                $query->sortby( 'created', 'DESC' );
                $query->where( 'owner_id', $idArray );
                $userStream = $modx->getCollection( 'UserPosts', $query );

                The above code won't return any results.
                • discuss.answer
                  • 49407
                  • 159 Posts
                  I just did a facepalm after I finally read the documents on query (was searching the wrong search terms). I was simply missing this ":IN".

                  For anyone else here is the solution...

                  $query = $modx->newQuery( 'ClassName' );
                  $query->sortby( 'created', 'DESC' );
                  $query->where( 'owner_id:IN', $idArray ); <- the ":IN"
                  $userStream = $modx->getCollection( 'ClassName', $query );

                  Bob, thanks again for your timely attention. I apologize for my overlooking the very simple solution and taking up your valuable time. I sincerely appreciate your help, always.
                    • 3749
                    • 24,544 Posts
                    I'm glad you got it sorted. smiley
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting