We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Shaun and I were discussing some issues with duplicate Property Set names and I thought I’d bring it up here to broaden the discussion.

    I discovered it when I couldn’t find a way in a snippet to get a named property set associated with an element (perhaps a separate issue) short of a brute-force iteration through the attached sets looking for the name. (There may be an easier way using getOne() but I haven’t found anything that works).

    I originally got the set with:

    $modx->getObject('modPropertySet',array('name'=>'SetName'));


    I realized, though, that this won’t work if more than one set has the same name. I think we’ve been assuming that many elements would have a Property Set called "default" so there’s a problem. It gets worse when you consider that in Tools | Property Sets, you can divorce a set from all elements, potentially leaving you with a bunch of sets called "default" and no way to tell them apart without looking at the properties themselves.

    We can’t prefix the element names to them because they can be attached to more than one element.

    As an alternative, we could require unique names for property sets, but then what happens if two add-ons each try to add a "default" property set during the package install? They could be renamed to default1, default2, etc., but that creates its own problems (like what happens on reinstall and update, for example).

    I think that the element_property_sets table solves the duplicate name problem somewhat, so maybe we just encourage people to use a prefix (or suffix) with their property sets, allow duplicates names, and hope things don’t get out of hand.

    That would mean that $getObject() couldn’t be counted on to find them, so we’d probably want to make sure that
    $element->getOne('modPropertySet',array('name'=>'setName'));

    would retrieve a named set (it doesn’t appear to now).

    I hope this makes sense and I’m not missing something obvious.


      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
      • 22303 MODX Staff
      • 10,725 Posts
      The default properties are not a Property Set, they are attached directly to the element. Just don’t call any additional property sets ’default’ to avoid confusion. Property Set names themselves must be unique, but I do not see any issue with this. The getOne() method would never work to get a property set directly from an element, because they are related through another table that gives them a one-to-many relationship, modElementPropertySet, with an alias of PropertySets. i.e.
      $attachedPropertySets = $element->getMany('PropertySets');
        • 3749
        • 24,544 Posts
        Quote from: OpenGeek at Aug 06, 2009, 10:20 AM

        The default properties are not a Property Set, they are attached directly to the element. Just don’t call any additional property sets ’default’ to avoid confusion. Property Set names themselves must be unique, but I do not see any issue with this. The getOne() method would never work to get a property set directly from an element, because they are related through another table that gives them a one-to-many relationship, modElementPropertySet, with an alias of PropertySets. i.e.
        $attachedPropertySets = $element->getMany('PropertySets');


        Ahh . . . that makes sense. Part of my confusion was semantic, then, since they are properties and there is a set of them. wink

        They’re clearly not a modPropertySet object, though. This will take some careful documentation to prevent confusion. It looks $element->getProperties() gets the same thing as what you get with $propertySet->get(’properties’) or $propertySet->getProperties(). Is that correct?




        Also, about getting a named property set of an element; this is all I’ve been able to come up with. It seems there must be a more direct way (other than using getObject() to search through all property sets):

        $setName = 'desiredPropertySetName';
        $chunkName = 'desiredChunkName';
         
        $object = $modx->getObject('modChunk',array(
        
                'name' =>$chunkName));
         
        $propsets = $object->getMany('PropertySets');
         
        foreach($propsets as $props) {
           $set = $modx->getObject('modPropertySet',$props->get('property_set'));
           $name = $set->get('name');
           if ($name == $setName) {
              break;
           }
        }
          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
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: BobRay at Aug 06, 2009, 05:30 PM

          They’re clearly not a modPropertySet object, though. This will take some careful documentation to prevent confusion. It looks $element->getProperties() gets the same thing as what you get with $propertySet->get(’properties’) or $propertySet->getProperties(). Is that correct?
          Agreed on clarifying the difference carefully. But there is a difference between getting the raw data with $propertySet->get(’properties’), and the ready to use key/value pairs with $propertySet->getProperties(). The former will return a more complex array for each key with all of the metadata associated with each property, while the latter has just the values, ready to use.

          Quote from: BobRay at Aug 06, 2009, 05:30 PM

          Also, about getting a named property set of an element; this is all I’ve been able to come up with. It seems there must be a more direct way (other than using getObject() to search through all property sets):
          There is no real need to go through the getMany() method here. Since PropertySets are unique by name, you can grab it directly by name and be done:
          $chunk = $modx->getObject('modChunk', array('name' => 'MyChunk'));
          $propSet = $modx->getObject('modPropertySet', array('name' => 'MyNamedPropertySet'));
          $parsedContent = $chunk->process($propSet->getProperties());
            • 3749
            • 24,544 Posts
            Quote from: OpenGeek at Aug 06, 2009, 07:47 PM

            Agreed on clarifying the difference carefully. But there is a difference between getting the raw data with $propertySet->get(’properties’), and the ready to use key/value pairs with $propertySet->getProperties(). The former will return a more complex array for each key with all of the metadata associated with each property, while the latter has just the values, ready to use.

            Thanks. Do $element->getProperties() and $propSet->getProperties() return equivalent arrays?

            There is no real need to go through the getMany() method here. Since PropertySets are unique by name, you can grab it directly by name and be done:
            $chunk = $modx->getObject('modChunk', array('name' => 'MyChunk'));
            $propSet = $modx->getObject('modPropertySet', array('name' => 'MyNamedPropertySet'));
            $parsedContent = $chunk->process($propSet->getProperties());


            Right, but that search involves every property set at the site. I thought it might be faster and more efficient to just get the few associated with the element and search them.
              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
              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: BobRay at Aug 07, 2009, 12:31 AM

              Thanks. Do $element->getProperties() and $propSet->getProperties() return equivalent arrays?
              Yes.
              Quote from: BobRay at Aug 07, 2009, 12:31 AM

              Right, but that search involves every property set at the site. I thought it might be faster and more efficient to just get the few associated with the element and search them.
              It’s a single query and only one result/object loaded in memory, which will always be more efficient than two queries returning multiple objects in memory and looping through them in PHP.
                • 3749
                • 24,544 Posts
                Right, I was looking for a shortcut that would get them more directly from the elements attached props without the loop.

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

                I’ve more or less finished laying out what I think I know here:

                http://bobsguides.com/revolution-objects.html

                Could you take a look at it and make sure I haven’t got any of it wrong before I announce the page to the world?
                  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