We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14877
    • 110 Posts
    I was trying to keep my code compact, so I had the following code fragment in a snippet:
    	/* Everything is fine, so create the new vendor profile */
    	$defaultValues['vendor_id'] = $addVendorId;
    	$theNewVendor = $zcDatabase->newObject('Vendor', $defaultValues);
    	if ($theNewVendor->save()):

    That semi-successfully created the row in the correct table; however, the key field ('vendor_id') was set incorrectly (it appeared to be set to an empty value, not NULL).

    I deleted that row using phpMyAdmin and changed the code to following:
    	/* Everything is fine, so create the new vendor profile */
    	$defaultValues['vendor_id'] = $addVendorId;
    	$theNewVendor = $zcDatabase->newObject('Vendor');
    	foreach ($defaultValues as $theCol => $theValue):
    		$theNewVendor->set($theCol, $theValue);
    	endforeach;
    	if ($theNewVendor->save()):
    

    Which works perfectly. So why does the first not work?

    I'm simply curious. I don't really mind coding the long way (at least it's clear what the code is doing).

    This question has been answered by BobRay. See the first response.

      __________________
      JRG
    • discuss.answer
      • 3749
      • 24,544 Posts
      There are a number of MODX objects that you can't set some fields of in newObject(). I've never been curious enough to figure out why. Your second method always works, as does using $object->fromArray(). The optional arguments to it are somewhat opaque, but often, you don't need them.

      In your case, that would be:


      $theNewVendor = $zcDatabase->newObject('Vendor');
      $theNewVendor->fromArray(array(
          'vendor' => $addVendorId,
      ));
      if ($theNewVendor->save()):


      If you haven't overridden the set() method in your class and the field values don't need any massaging (i.e., no date or JSON fields), you can speed thing up a little by using the raw values:

      $theNewVendor = $zcDatabase->newObject('Vendor');
      $theNewVendor->fromArray(array(
          'vendor' => $addVendorId,
      ), "", false, true);
      if ($theNewVendor->save()):


      Without the last argument (true), fromArray() will call set() on each value.
        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
        • 14877
        • 110 Posts
        Hi Bob,

        Neither of the ways you suggest, have the array $defaultValues mentioned. That is an array that includes default values for every column in the table (which is why I set the 'vendor_id' entry).

        When I originally defined the table, I decided to require values to be set explicitly, so the default values are, for the most part, "empty" (not NULL).

        But your comment does explain the issue. Thanks for taking the time, it is appreciated.
          __________________
          JRG
          • 3749
          • 24,544 Posts
          Ah, in that case it would be:

          $theNewVendor = $zcDatabase->newObject('Vendor');
          $theNewVendor->fromArray($defaultValues, "", false, true);
          if ($theNewVendor->save()){
             /* Something here */
          }
            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