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

    is there any way to create a query like the following?
    UPDATE `ft_links` SET `clicks` = clicks+4 WHERE `id` = 1


    I tried something like this setting the click’s phptype either to string or to integer but I didn’t have any luck.
    $link->set('click', 'click+3');


    Thanks in advance.
    Nick
      • 28215
      • 4,149 Posts
      $link = $modx->getObject('ftLink',1);
      $link->set('clicks',($link->get('clicks')+4));
      $link->save();

        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 21299
        • 0 Posts
        Quote from: splittingred at May 11, 2011, 03:41 PM

        $link = $modx->getObject('ftLink',1);
        $link->set('clicks',($link->get('clicks')+4));
        $link->save();



        It’s not exactly the same. Concurrent updates could cause a conflict.
          • 21299
          • 0 Posts
          I guess I have to do a $xpdo->exec($sql) although I am missing the caching functionality.
            • 22303 MODX Staff
            • 10,725 Posts
            The method splittingred told you is correct and it is the same. The save() method creates an UPDATE statement almost exactly like the one you are saying you need to send using exec(), and will update the db result set cache.
              • 21299
              • 0 Posts
              First of all thanks to both of you! You are really helpful and very responsive.
              I understand that the method you are suggesting will work. I just have some considerations applying this method on a high traffic environment. Theoretically, two different users could concurrently cause the same query. This query would be served by two servers, or two threads, with different loads. Calculating the count variable on the PHP side and not on the DB side could cause a wrong update.
              Probably my question is owed to bad application by myself, which I have to reconsider.