We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6911
    • 2 Posts
    Hi all. I’m a newbee.

    I’ve just read this document.
    http://www.xpdo.org/announcements/important-changes-in-1.0-beta.html

    The last feature "Hydrating object variables" is very very interesting.
    Can you please provide me some tips, some code snippets explaining me how it can be implemented, please?

    Thanks in advance,
    Daniele
      • 28215
      • 4,149 Posts
      In revo, it’s already done. If you’re using xPDO by itself, you’d pass into the config array:

      XPDO_OPT_HYDRATE_FIELDS => true,
      XPDO_OPT_HYDRATE_RELATED_OBJECTS => true,
      XPDO_OPT_HYDRATE_ADHOC_FIELDS => true,
      


      XPDO_OPT_HYDRATE_FIELDS:
      Makes it so you can access fields straight instead of using get:
      $chunk->get('description');
      // becomes
      $chunk->description;
      


      Note: MODx recommends, however, to still use ->get(), since that allows for upgradability in the future.

      XPDO_OPT_HYDRATE_RELATED_OBJECTS:
      Allows you to directly access related objects when grabbed:

      $profile = $user->getOne('modUserProfile');
      $profile->get('email');
      
      // becomes
      $user->getOne('modUserProfile');
      $user->modUserProfile->get('email');
      



      XPDO_OPT_HYDRATE_ADHOC_FIELDS:
      Allows you to hydrate adhoc (non-db-column) fields as well:

      $user->set('test',1);
      $t = $user->get('test');
      
      // becomes
      $user->set('test',1);
      $t = $user->test;
      


      How is this useful? Well, we still recommend using ->get() for things, since that allows us to easily override aliases later should we need to

      However, templating engines like Smarty can use hydration to their benefit...like in the Revo manager:

      In the controller:
      $modx->smarty->assign('user',$user);
      

      In the template:
      Username: {$user->username}
      


      Hope that helps.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 6911
        • 2 Posts
        Sounds very handy smiley
        Thank splittingred very much...great how-to!