We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39240
    • 2 Posts
    I really like some of the features I'm starting to understand about MODX but I think my brain is caught in other modes of database design, so I'm having trouble figuring out how I would create something I need in MODX.

    What I want to do is to create an author and book database. One author (or illustrator) would have one or dozens of books assigned to them. Is there an example of something simliar I could decompile or could someone steer me on to some resources that might help me figure this out. I'm not sure if MODX can handle this directly or if I need to understand something like PDO.

    Thanks!
      • 3749
      • 24,544 Posts
      You would need to use either PDO or xPDO and create some custom DB tables because you have many-to-many relationships.

      Since a book can have more than one author and an author can have more than one book, you'll need some intersect tables in addition to the main tables:

      authors - author info (id, name, etc.)
      illustrators - illustrator info (id, name, etc.)
      books - book info (id, title, ISBN, pages, Edition etc.)
      booksIllustrators - contains pairs of book IDs and Illustrator IDs
      booksAuthors - contains pairs of book IDs and Author IDs

      xPDO is built into MODX Revolution and would make things more convenient and efficient, but you need to create class and map files for your various objects and learn how to use it. That wouldn't be necessary with PDO.

      Here's some info on using xPDO: http://rtfm.modx.com/display/revolution20/Using+Custom+Database+Tables+in+your+3rd+Party+Components



      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      MODX info for everyone: http://bobsguides.com/modx.html
        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
        • 39240
        • 2 Posts
        Bob,

        I'm glad you were the one to answer--I had just ordered your new book. It arrived a couple of days ago but I've been out of town. I'm looking forward to pouring over the book to fill my brain up with as much MODX as I can stand. It's great to have such a good reference.

        I'm using MODX Revolution 2.2.0-pl2. I don't understand the difference between PDO and xPDO, so I'll research that first. From your answer, it sounds like xPDO is built into MODX but PDO is not, even though PDO would be easier to use? At this point I'm in favor of easier, although I the relation between the tables I'll have is a bit complex at times.

        Thanks,
        Steve

          • 3749
          • 24,544 Posts
          PDO is available on any server, xPDO is built into MODX (and Revolution is based on xPDO). xPDO was developed by MODX core developer Jason Coward (OpenGeek).

          The "which is easier" question is not a simple one. If you already know PDO, it's easier, but if you don't, xPDO is easier to *use* IMO.

          xPDO takes a little work to set up (whereas, PDO will work right away). For xPDO, you have to set up a schema for your custom tables and then generate class and map files. Once you do that, though, it's relatively easy to work with.

          This page (http://bobsguides.com/revolution-objects.html) will give you some of the flavor of using xPDO. Once you have things set up, your "objects" (which are basically rows in your tables) have the same status as the MODX objects described on this page and you can get them with the same methods. For example, with xPDO you could get a book with this:

          $book = $modx->getObject('modBook', array('title' => 'Gone With the Wind'));


          You could then get the book's authors or illustrators like this:

          $authors = $book->getMany('Authors');


          You'll get back an array of author objects (that might have just one member).

          Similarly:

          $illustrators = $book->getMany('Illustrators');


          You could get *all* books with this:

          $books = $modx->getCollection('modBook');


          Going the other way, if you had an $author object, you could get that author's books with:

          $author->getMany('Books');




          I hope that makes sense.

          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html

            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