We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20380
    • 118 Posts
    There is a mysql query:
    UPDATE table SET value = n WHERE k = n

    There is many lines "where k=n" in table. It’s not $xpdo->getObject. Then how?


    (sorry for bad English embarrassed)
      • 17499 ☆ A M B ☆
      • 872 Posts
      Try the following:

      <?php
      /* the array is like a where statement */
      $yourTable = $modx->getObject('yourTable',array(
      	'k' => 'n',
      ));
      $yourTable->set('key', 'value');
      $yourTable->save();
      


      You can find more information on the xPDO documentation here
        • 20380
        • 118 Posts
        I’d already try it - changed only 1 line, not all selected. That’s why I ask rolleyes))
          • 20380
          • 118 Posts
          And I’d try
          <?php
          $yourTable = $modx->getCollection('yourTable',array(
          	'k' => 'n',
          ));
          $yourTable->set('key', 'value');
          $yourTable->save();

          - Error
            • 21257 MODX Staff
            • 730 Posts
            To do it with a collection, you’d have to iterate the collection, setting and saving each member object.
            It would be more efficient just to use the fact that xPDO wraps PDO.
            $table_name = $modx->getTableName('table');
            $result = $modx->query("UPDATE {$table_name} SET {$modx->escape('value')} = 'n' WHERE {$modx->escape('k')} = 'n');
            

            So $modx->query is wrapping PDO::query http://ca3.php.net/manual/en/pdo.query.php

            Also note the use of $modx->escape to properly escape field names.. this will help to make sure your work will be portable across database drivers, as MODX starts to support that in the upcoming 2.1 release. (Assuming, of course, that the syntax of your SQL statement itself is portable ;-)
              Mike Schell
              Lead Developer, MODX Cloud
              Email: [email protected]
              GitHub: https://github.com/netProphET/
              Twitter: @mkschell
              • 20380
              • 118 Posts
              I’d already did it like this wink , but thought, there is another way do it undecided
                • 17499 ☆ A M B ☆
                • 872 Posts
                @Mraymax

                My mistake.

                To expand on your last comment, it would be nice to be able to update several fields the xPDO way.

                With a syntax like :

                <?php
                $qry = $modx->updateCollection('className', $where);
                


                Does it worth a feature request ?
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Quote from: lossendae at Mar 29, 2011, 11:31 AM

                  @Mraymax

                  My mistake.

                  To expand on your last comment, it would be nice to be able to update several fields the xPDO way.

                  With a syntax like :

                  <?php
                  $qry = $modx->updateCollection('className', $where);
                  


                  Does it worth a feature request ?
                  I certainly think it is...er, does, er, whatnot...
                    • 17499 ☆ A M B ☆
                    • 872 Posts
                    English was misused here ?
                    What whatnot means ?

                    Seriously, i’m lost... :p




                      • 21257 MODX Staff
                      • 730 Posts
                      LOL, translation: Yes, it’s worth opening a feature request ticket for that.
                      :D
                        Mike Schell
                        Lead Developer, MODX Cloud
                        Email: [email protected]
                        GitHub: https://github.com/netProphET/
                        Twitter: @mkschell