We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40092
    • 265 Posts
    Hi guys,

    I am trying to figure out how to UPDATE a custom table in xpdo, but i get kinda stuck in the documentation.

    In Evolution i would have done something like this:

    $fields = array(
    	
    	'naam' => mysql_real_escape_string($_POST['naam']),
    	'code' => mysql_real_escape_string($_POST['code']),
    	'intro' => mysql_real_escape_string($_POST['intro']),
    	'impressie' => mysql_real_escape_string($_POST['impressie']),
    	'prijs' => mysql_real_escape_string($_POST['prijs']),
    	'alias' => mysql_real_escape_string(strtolower($alias1)),
    	'actief' => mysql_real_escape_string($_POST['actief']),
    	'kamertypes' => mysql_real_escape_string($_POST['kamertypes']),
    	'hotel_info' => mysql_real_escape_string($_POST['hotelinfo']),
    	'aanbiedingen' =>	mysql_real_escape_string($_POST['aanbiedingen']),
    	'dag_tot_dag' => mysql_real_escape_string($_POST['dagtotdag']),
    	'reisinformatie' => mysql_real_escape_string($_POST['reisinformatie']),
    	'overige_informatie' => mysql_real_escape_string($_POST['overigeinformatie']),
    	'meta_description' => mysql_real_escape_string($_POST['meta_description']),
    	'plaats' => mysql_real_escape_string($_POST['plaats']),
    	'frontpage' => mysql_real_escape_string($_POST['frontpage']),
    	'soort' => mysql_real_escape_string ($soort),
    	'set_aanbieding' => mysql_real_escape_string ($_POST['set_aanbieding']),
    	'gewijzigd' => UNIX_TIMESTAMP(),
    	);
    
    
    $result = $modx->db->update($fields, 'Reizen', 'id' = $_GET['nid']);


    But that does'nt fly in Revolution.
      • 6038
      • 228 Posts
      XPDO is the preferred way to interact with your database in MODX but, as you probably know, requires a bit more to set up for a custom table. To use in the same way as you are accustomed to in Evolution, you can use the methods shown here:

      http://rtfm.modx.com/display/xPDO20/xPDO.query
        • 3749
        • 24,544 Posts
        If you just have a single table, you might find this useful: http://bobsguides.com/custom-db-tables.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
          • 40092
          • 265 Posts
          @christianhanvey Thanks, but that's the place i got stuck ;-) I was looking for some example of how to do an UPDATE statement the XPDO way. I was not able to find that.

          @BobRay Thanks, i already had this one bookmarked for life;-) I put multiple custom tables in this way. smiley Like i said above, i was looking for some sort of an example how to do an UPDATE statement the XPDO way.
            • 18373 ☆ A M B ☆
            • 3,141 Posts
            With xPDO, you don't do UPDATE or SELECT etc; you interact with objects that just happens to update the tables. Different paradigm.

            So if you set up your schema and model, you could use something like this:

            $object = $modx->getObject('myObject', 15); //Gets (ie SELECT) the myObject with primary key 15
            $object->set('fieldname' , $newValue);
            $success = $object->save(); // Updates the table


            For many fields, you could do this:
            $fields = array(
              'field' => 'value',
              'foo' => 'bar',
              'etc' => 'etc',
            );
            $object = $modx->getObject('myObject', 15);
            $object->fromArray($fields);
            $success = $object->save();

              Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

              Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              With xPDO, you don't do UPDATE or SELECT etc; you interact with objects that just happens to update the tables. Different paradigm.
              Indeed, working with xPDO you need to learn an inverted way at looking at your data manipulation. It's not database-centric, it's data-centric. You don't worry about the database, xPDO takes care of it for you.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 40092
                • 265 Posts
                [SOLVED]

                Thanks people, again, learned a lot. [ed. note: thapriest86 last edited this post 13 years, 10 months ago.]
                  • 42681
                  • 64 Posts
                  But what about mysql_real_escape_string?

                  Is this still necessary or is this kind of cleaning done by xpdo automatically on the flow?

                  And the same question regarding htmlspecialchars? Is this still necessary when writing content into a modx-database when this content is returned later on a webpage?
                    • 42602
                    • 81 Posts
                    xPDO uses PDO and prepared statements http://php.net/manual/en/book.pdo.php, all data is escaped accordingly your schema rules (dbtype="string") etc... already with xPDO. Or more of are set to PDO statement with correct constants and quite lot of the rest is handled with PDO. This is obviously true only when you do not set variables directly to query strings, then they are not escaped by PDO. Eg: "SELECT * FROM somewhere WHERE something = '$myVar'"; <--- the $myVar would not get escaped. This kind of queries are possible with xPDO but rarely seen in ordinary days work.

                    Also there is xPDO::updateCollection() method that looks like : updateCollection($className, array $set, $criteria= null). So basically you could update one row with $modx->updateCollection('Reizen_i_guess', $fields, array('id'=>$_GET['nid'])); Array could be only the primary key $_GET['nid'] also. The $_GET variable will get escaped with the xPDO/PDO. Your duty is to make sure it is valid and people do not just inject random numbers and stuff.

                    And for the function mysql_real_escape_string() would not work as this function requires mysql connection done with the mysql_ library.