We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49324
    • 14 Posts
    Hello, i need to get the id of a resource that has to be found based on the value of one of its template variables.

    I was trying something like:
    $ctx = $modx->getContext('contextName');
    
    $document = $modx->getObject('modResource',array(
        'context_key' => $ctx,
        'TVname' => 'TVvalue'
    ));
    $document_id = $document->get('id');


    Where TVname is the name of the template variable in that resource and TVvalue is the value i want to use as "filter".
    I suppose this part
    'TVname' => 'TVvalue'
    is wrong...can you point me in the right direction? Many thanks.
      • 3749
      • 24,544 Posts
      TV values are not stored in the DB table that has the resources, so it's a little trickier.

      If the TV is never set to its default value, and doesn't require processing (i.e. it's a regular text TV), and you know that there's only one resource with that TV value, this will be a very fast efficient way to get the resource (replace 12 with the ID of the TV).


      $tvId = 12;
      $tvValue = 'some value';
       
      /* Get the resource ID from the modTemplateVarResource */
      $query = $modx->newQuery('modTemplateVarResource', array(
          'value' => $tvValue,
          'tmplvarid' => $tvId,
      ));
      $query->select('contentid');
      $DocId = $modx->getValue($query->prepare());


      BTW, TVs are not a very good place to store resource selection criteria because of the way they're stored. If there is an unused resource field (introtext, description, menutitle), it will be a lot faster and easier to put the data there.

      If you are using multiple TVs this way, you might be better off extending the modResource object with ClassExtender to add your extra fields (which could then be shown as regular resource fields on the Create/Edit Resource panel).
        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
        • 49324
        • 14 Posts
        Thanks again Bob for you help and effort.
        For now i'm using the code you shared but i'm looking forward to use the ClassExtender extra, for this i have a couple of questions:

        • Once the Extend modResource has been viewed and ExtraResourceFields plugin enabled and some of the resources have been created/saved, will it be possible to change or add fields without deleting the associated db table? If yes, do you a have a suggested method?
        • In the chunk MyExtraResourceFields is it possible to use also rich text fields (i am currently using tinyMCE editor)? and image type input to enable image upload or select?
        thanks again.
          • 3749
          • 24,544 Posts
          1. You can add fields to the table or change their names, but I think you'd have to create a new schema and new class and map files. I'd back up the table before running anything.

          2. I've never tried this, but It should be possible and just a matter of setting the correct class on the text field that CE adds to the Create/Edit Resource panel. Do a "view source" on that page with rich text turned on for the content field and see if you can tell what the correct class is. You may have to view the source in Firebug, since much of it is created on the fly by ExtJS/modExt.
            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