We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 54163
    • 24 Posts
    I'm writing a script for listing users. I use ClassExtender to store extra fields and information about which fields the user wants to disclose. I'm familiar with getCollectionGraph, but because I want to use pdoPage and get best speed I think pdoFetch would be best. Does anyone know of a good snippet to learn from and be willing to share?

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

      Fake News is powered by MODX — https://fakenews.com/
      • 3749
      • 24,544 Posts
        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
        • 54163
        • 24 Posts
        Yes. I think I can get getCollection working, but I was thinking more about getCollectionGraph. Some way of doing
        $c = $modx->newQuery('userData');
        $c->where(array('User.active' => '1'));
        $users = $modx->getCollectionGraph('userData', '{"Profile":{},"User":{}}', $c);

        if we speak about ClassExtender. But it would be nice to know how to use it for any class.
          Fake News is powered by MODX — https://fakenews.com/
          • 3749
          • 24,544 Posts
          I'm not sure I understand your question. The topic is pdoFetch, but you're asking about getCollectionGraph().

          As far as I know there is a $pdo->getCollection, but there's no $pdo->getCollectionGraph().

          $modx->getCollectionGraph() is quite fast and efficient in my experience, so even if you could somehow do the equivalent with pdoTools, I don't think you'd save much time unless you're querying massive amounts of data.
            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
            • 54163
            • 24 Posts
            If I rephrase it as "How to get paged result with pdoTools from any data with related objects"?

            Can pdoResources and pdoUsers handle extended data? There are &loadModels and &class parameters, but how do I use them for example with ClassExtender?


            BTW, pdoTools is not necessary, but it would be nice to master it.
              Fake News is powered by MODX — https://fakenews.com/
              • 3749
              • 24,544 Posts
              I tend to write custom code for stuff like that, so I haven't used pdoTools much at all. Your question makes more sense to me now that I know that pdoFetch is a class that you can extend or replace for use with any of the pdoTools snippets.

              Can pdoResources and pdoUsers handle extended data?

              I'd say definitely yes, but I don't know how to do it. That said, it's more complicated since ClassExtender's classes do not extend modUser and modResource. So, IIRC, the resource and the user (and maybe the profile) are related objects of the class extender objects, but not the other way around.

              There's an example here, that does a join, but I think it's untested.

              You probably know this, but ClassExtender has getExtUser and getExtResources snippets that may do what you want. If they don't, you can duplicate them and modify them to meet your needs. You can also look at them as examples of how to get related object data for users and resources.

                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
              • discuss.answer
                • 54163
                • 24 Posts
                Bob! You were quoted in a thread I just read:
                Quote from: BobRay
                If (and it's a big if) you've properly created the tables as related objects of each other, you can use getCollectionGraph() to get them all in a single query. You can also use $query->leftJoin() with getCollection().
                That was what I was missing. Lesson learned: getCollectionGraph() is handy through it's use of model definitions, but with some extra typing getCollection() can do the job too.

                After examining the pdoTools code I figured out how to do it. No need to make a new snippet. pdoUsers has it all.
                [[!pdoPage?
                    &element=`pdoUsers`
                    &leftJoin=`[{"class":"userData", "alias": "userData", "on":"userData.userdata_id = modUser.id"}]`
                    &select=`{"userData": "firstName, lastName"}`
                    &loadModels=`{'extendeduser': MODX_CORE_PATH.'components/classextender/model/'}`
                    &toPlaceholder=`userlist`
                    &tpl=`userItem`
                    &tplWrapper=`usersWrapper`
                    &setTotal=`1`
                    &totalVar=`total`
                    &showInactive=`0`
                    &showBlocked=`0`
                    &showLog=`1`
                ]]
                [[!+userlist:notempty=`[[!+userlist]][[!+page.nav]]`]]
                  Fake News is powered by MODX — https://fakenews.com/
                  • 20413
                  • 2,877 Posts
                  Quote from: BobRay at Oct 31, 2018, 08:44 PM
                  You've seen this, right?

                  https://docs.modx.pro/en/components/pdotools/classes/pdofetch

                  WOW!! Thanks!! 8-)
                    @hawproductions | http://mrhaw.com/

                    Infograph: MODX Advanced Install in 7 steps:
                    http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                    Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                    http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
                    • 3749
                    • 24,544 Posts
                    @Hans Svensson: That looks really good. What most people do is get all the resources, then loop through them to get the related objects, which obviously means many, many queries to the DB. Your method gets everything in a single query.

                    @mrhaw: I'm glad I could help. 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
                      • 54163
                      • 24 Posts
                      A sidenote: I still don't get the paging right, but I think I know why. The pdoPage snippet relies on
                      $total = (int)$modx->getPlaceholder($totalVar);

                      That is probably a trick (preferred way?) to pass a number between pdoPage and the element used, but in my case something else intercepts the placeholder.
                        Fake News is powered by MODX — https://fakenews.com/