We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39521
    • 15 Posts
    You'll have to forgive me as I'm still quite new to MODx. I'm working with Revo 2.2, I've managed to create a form that inserts data into a table using the xPDO model (That took me like 6 hours to figure out sad). The only problem is I can't figure out how for the life of me how to get the data back out.

    This is the code I have right now just trying to get work:
    
    $base_path = !empty( $base_path ) ? $base_path : $modx->getOption( 'core_path') . 'components/events/';
    $modx->addPackage( 'events', $base_path . 'model/' );
    
    $entries = $modx->getCollection( 'events', array( 'event_id' => 5 ), FALSE );
    
    echo "Pre foreach<br />\n";
    foreach( $entries as $entry )
    {
    	echo "|-------------------------<br />\n";
    	echo "|- Entry ID: " . $entry->get('entry_id') . "<br />\n";
    	echo "|- Event ID: " . $entry->get('event_id') . "<br />\n";
    	echo "|- Name: " . $entry->get('name') . "<br />\n";
    	echo "|- Email: " . $entry->get('email') . "<br />\n";
    	echo "|-------------------------<br /><br /><br />\n\n\n";	
    }
    
    echo "End foreach<br />\n";
    


    I have this running in a snippet and it doesn't error, it just doesn't pull anything out.

    Can someone point me in the right direction?
      • 33968
      • 863 Posts
      What's your package and class name? Instead of using addPackage in your snippet, you need to use something like this otherwise MODX doesn't know where to get your object model from:
      $events = $modx->getService('events','Events',$modx->getOption('core_path').'components/events/model/events/');
      if (!($events instanceof Events)) return '';
      


      Check out the snippet in the Doodles tutorial for an example.
        • 39521
        • 15 Posts
        It'd probably be events, events.

        Once I've gotten it instantiated how do I go about querying?
          • 33968
          • 863 Posts
          $entries = $modx->getCollection('events');

          That will retrieve all events, as long as 'events' is the class name you have specified in your schema. If you follow page one of the Doodles tute it will take you step by step through what you need to do.