We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9721
    • 2 Posts
    I have experience with php and mysql, but none with MODx. Question: can I create a few of my own tables on the MODx database? And then use your DBAPI to manipulate those tables? For example, I might want to do a left join on two of my tables and insert the results into a third table. Could I do something like that using your DBAPI? And would that be properly done within a snippet?

    Thanks
      • 7231
      • 4,205 Posts
      Yes, you could even use the dbapi to access a second db. Snippets like MaxiGallery and Jot both do this. The proper method depends on the objectives. Here is an example using db->query:
      $output = '';
      $result = $modx->db->query( 'SELECT id, name, joined FROM `user_table` GROUP BY `member_type` ORDER BY name ASC' );
       
      while( $row = $modx->db->getRow( $result ) ) {
      	$output .= '<br /> ID: ' . $row['id'] . '<br /> Name: ' . $row['name'] . '<br /> Joined: ' . $row['joined']
      	. '<br />---------<br />';
      }
      return $output;


      The wiki has good dbapi documentation and examples:
      http://wiki.modxcms.com/index.php/API:DBAPI

      and the official docs also have a few good examples as well:
      http://modxcms.com/dbapi.html
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don&#39;t know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 9721
        • 2 Posts
        Okay, thank you, dev_cw. My idea is to put my own tables in the MODx db so that I can join up with MODx tables to get some kind of special functionality. Is there anything wrong with that idea?
          • 7231
          • 4,205 Posts
          No, it is a great idea. Depending on your objectives you may be interested in checking out plugins and the events. You can hook into several events of the document processing (and other stuff). This may be handy in getting the data to ’work’ at the right time.
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don&#39;t know what it is.
            Do you, Mr. Jones? - [bob dylan]