We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    When using the save method, do I have to snoop around to make sure I’m updating an object instead of creating a new one? If I create an object and I specify an existing primary key:

    $wand = $xpdo->newObject('Wand');  
    $wand->set('primary_key', 123);
    if ($wand->save() == false) {  
    
    }


    Does this update the object in question?
      • 21257 MODX Staff
      • 730 Posts
      I would do it like this:
      $wand = $xpdo->getObject('Wand', 123);
      if($wand === null) {
        $wand = $xpdo->newObject('Wand', array('primary_key'=> 123));
      }
      $wand->save();
      

        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 17499 ☆ A M B ☆
        • 872 Posts
        +1 netProphet

        I use a similar method:
        <?php
        $wand = $xpdo->getObject('Wand', 123);
        if(!$wand) {
        	$wand = $xpdo->newObject('Wand', array('primary_key'=> 123));
        }
        $wand->set('primary_key', 123);
        if($wand->save()){
        	/* My code */
        }
        
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          Ok... but as to my question: no matter how to quantify which object you loaded up, does that method result in an UPDATE? Or does it attempt to create a NEW object?
            • 21257 MODX Staff
            • 730 Posts
            Pretty sure that if you have a ->newObject()
            it’s always going to result in an INSERT so if you assign the pk of an existing object, you’ll have a problem.
              Mike Schell
              Lead Developer, MODX Cloud
              Email: [email protected]
              GitHub: https://github.com/netProphET/
              Twitter: @mkschell
              • 28215
              • 4,149 Posts
              Well, actually, it will only assume it’s not new if you got it from getObject. But, you should *only* assign PK’s if you not extending xPDOSimpleObject.

              That said, you can check if an object is new via:

              if ($obj->isNew()) { 
                shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • 49407
                • 159 Posts
                Raising the dead here but, reading this gives me the impression that there is no class extension to update a record?

                So, do I have to just use the regular beginTransaction and commit methods?
                  • 3749
                  • 24,544 Posts
                  I've never used transactions (the MyISAM engine doesn't support them). I don't see why they would be necessary.

                  AFAIK, if you get the object with $modx->getObject(), the save() will result in an update automatically. If you intend to create a new object, it's good form to call getObject() first to make sure it doesn't already exist. The return value of getObject() will be empty if the object doesn't exist and you can then safely call newObject() and save it.
                    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
                    • 49407
                    • 159 Posts
                    Quote from: BobRay at Dec 10, 2014, 09:41 PM
                    I've never used transactions (the MyISAM engine doesn't support them). I don't see why they would be necessary.

                    Ah, so I can't even use transactions? Not even in my own class?

                    Quote from: BobRay at Dec 10, 2014, 09:41 PM
                    AFAIK, if you get the object with $modx->getObject(), the save() will result in an update automatically. If you intend to create a new object, it's good form to call getObject() first to make sure it doesn't already exist. The return value of getObject() will be empty if the object doesn't exist and you can then safely call newObject() and save it.

                    Seems simple enough. Can I pass an array with each column key->value like; newObject($fieldSetArray, $primary_key_value) and have set them all in one $dataobject->set call, or do I have to update each column individually using a foreach loop?
                      • 4172
                      • 5,888 Posts
                      try

                      if ($object=$modx->getObject($classname,array('yourfield'=>'yourvalue'))){
                      
                      }else{
                          $object=$modx->newObject($classname);
                      }
                      
                      if ($object){
                          $object->fromArray($yourvalues);
                          $object->save();
                      
                          //get the id of the newly created object
                          $newid = $object->get('id');
                      }


                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!