On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi everyone! Pretty straight forward. I'm creating a custom schema where I've defined some classes that have aggregate relationships to the modResource class. This works fine when I want to get a modResource object from my custom class, but obviously this won't going the other direction. So the question is this: can I or should I extend the modResource class just so that I can define a composite relationship to one of my custom classes?

    Thanks!

    Jeff

    This question has been answered by multiple community members. See the first response.

    • It should be possible to define aliases in your schema that would allow something like this:

      $resource->getOne('customClassName');


      If you look at the modUser and modUserProfile classes in the MODX schema you'll see that you can do either of these:

      $profile = $user->getOne('Profile');
      $user = $profile->getOne('User');

      Notice the MODX convention that aliases begin with a capital letter.
      Remember that you need to regenerate the class and map files after changing the schema.

      Here's another way that won't require changing your schema and regenerating the classes. This assumes that your custom table records contain the ID of the resource in the 'internalKey' field (that's the convention):

      $customRecord = $modx->getObject('customClassName', 
          array('internalKey', $modx->resource->get('id')));



        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
      • Quote from: BobRay at Dec 04, 2018, 06:26 AM
        It should be possible to define aliases in your schema that would allow something like this:

        $resource->getOne('customClassName');

        Yes, thanks Bob absolutely what I'm after. But should I extend modResource just so that I can define the relationship? Is there a best practice around that? So for instance in my custom schema I might have the following:

        <object class="extModResource" extends="modResource">
            <composite alias="SomeCustomClass" class="extSomeCustomClass" local="id" foreign="resource_id" cardinality="many" owner="local" />
        </object>


        Notice that I just have the relationship and no other fields. Is this okay to do? Furthermore, if it is okay to do, do I have to include the methods in my custom class file as indicated by the modResourceInterface described below:

        interface modResourceInterface {
            public static function getControllerPath(xPDO &$modx);
            public function getContextMenuText();
            public function getResourceTypeName();
        }


        Essentially, what I'd like to do is extend the modResource class, define the composite relationship, but not have to create all the required methods that go along with extending the class. Possible?
        • discuss.answer
          I see what you're saying.

          You won't have to implement those methods if you extend modResource because it implements them already, and extending modResource may be a good idea if you want the convenience of an alias like you describe above, though I think your cardinality should be one unless it's a many-to-many relationship, which seems unlikely.

          You might want to look at the ClassExtender extra. Note that it doesn't actually extend the modResource object (to avoid issues with other extras that do). That means a little custom code to get your object from the resource object, but there is a snippet (getExtResources) like getResources that will get them for you and there are code examples in the docs. ClassExtender will automatically put your extra fields onto the Create/Edit Resource panel, and save them to the DB when you save the Resource, which is handy. The down side, iirc, is that when you delete a resource, the custom fields data is not automatically deleted.



            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
          • discuss.answer
            If all you're going to do is add the relation, that does seem like a very heavy hammer for your nail. The benefit of getOne is a small one, which could easily be replaced by a helper method on a service class like
            $service->getSomeCustomClassFor(modResource $resource)


            I've personally used custom resource types when the UI and other behavior of the resource also changed.
              Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

              Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • Awesome, thanks Bob and Mark! Bob I'll look into your extra suggestion and Mark I think you've confirmed my suspicions, it does seem heavy handed. Much appreciated!

              Jeff