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
    As I said above, can you explain in simple terms what you're trying to get? Your code appears to be trying to get all companies where the company ID is equal to the value of any TV on the whole site. Is that really what you want?
      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
      • 1331
      • 129 Posts
      Ah, good point. I need to also specify the exact TV in my query. I've just added that to my WHERE clause, and it's working.

      I'm trying to return any Template Variable whose numeric content matches any ID in the companies table. The companies table is being into this TV as a dropdown using the SELECT binding.

      On the front end, I'm returning the name of the items in the companies table. These click through to a page that has a taglister call that grabs all posts that have that company or set of companies assigned to it in the template variable.

      The part that I'm trying to solve now is that there are companies in the Companies table that haven't been assigned to any posts yet. So, we don't need to show them in the list of clickable companies.

      My idea was to join the tables and check for any company IDs that do not match the value column of the TV. But, as I'm seeing, like you mentioned earlier, if the TV has no value, it's just doesn't exist in the database.

      I was hoping that by using a left join, all companies would still be returned in the query, just without their paired items from the combined table. That's why I was checking for NULL values in the second table.
        • 1331
        • 129 Posts
        Ok, I just tested again and it does behave the way I imagined. When I do not specify the exact template variable to look within, I get the expect results: the companies from the Companies table are returned in spite of the fact that they do not have a matching column in the adjoining table.

        select ssm_companies.id as co_id, ticker, company_name, active, modx_site_tmplvar_contentvalues.* FROM
        	ssm_companies
        LEFT JOIN 
        	modx_site_tmplvar_contentvalues
        ON 
        	ssm_companies.id=modx_site_tmplvar_contentvalues.value
        WHERE
        	 active!=0
        order BY
        	ssm_companies.id


        Result:

        http://miscshare.s3.amazonaws.com/query_output.png [ed. note: ambaxter last edited this post 13 years, 1 month ago.]
          • 3749
          • 24,544 Posts
          That's a tough one. I don't see any reliable way to do it the way you're going at it, since as you say, the TVs won't exist for unassigned companies. You don't want to use all TVs because some other TV (possibly for an add-on component) could be set to a company ID. TVs are designed to add extra fields to a Resource. It's possible to use them for other purposes, but the fact that the modTemplateVarResource is often not set means that it won't work for a lot of use cases, especially when the TV is a multi-select one, which it sounds like yours is. Using LIKE isn't going to work because a company with the ID of 1 is going to get TVs set to 12, 13, 14, etc.

          I'm still not clear on what you're doing. I don't know what it means when the TV contains the ID of a company. It sounds like you're using Resources to store some company-related data, which is generally a bad idea. Resources are meant to provide a "view" of data, not to replace database tables.

          You might want to consider adding two more tables -- one to hold the information that's in the Resource, and another that serves as an intersect table (like the modTemplateVarResource table) with a field that points to an id in the new table and another field that holds the company ID.

          That way, you only need one Resource to display the information on any company in the front end (using a Tpl chunk to format the data), instead of one Resource for each company. It's faster and more efficient, and searching and sorting will be a lot easier.

          If feel you need the Resources as part of your user interface, you can create a plugin connected to OnDocFormSave that stores the data wherever you like when a Resource is saved.


            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
            • 1331
            • 129 Posts
            I hear ya. I may have to go back to the drawing board on this one. No, not using resources for that. It's a manually managed custom table. Each company is a new row. This, in turn feeds the TV where you pick companies.

            Thank for all your help on this. I'll keep playing with it and report back if any new developments arise!
              • 39404
              • 175 Posts
              stalemate resolution associate Reply #16, 13 years, 1 month ago
              ambaxter, from the looks of it, you're trying to determine which stock quotes exist in your external db versus what is in your tv values, probably for the purposes of trying to synchronize the two.

              If so, you could take the results of the query where the modx_site_tmplvar_contentvalues ids are all null (you can also just test the key field because that would mean they don't exist at all).

              Using this list you could do an insert like this:

              INSERT INTO modx_site_tmplvar_contentvalues (tmplvarid, contentid, value)
              SELECT (20, 0, co_id) 
              FROM      ssm_companies
              LEFT JOIN
                  modx_site_tmplvar_contentvalues
              ON
                  ssm_companies.id=modx_site_tmplvar_contentvalues.value
              WHERE
                   active!=0
              order BY
                  ssm_companies.id
              

              That would insert the new company ids from the stock quotes table, but you would have to then go back an manually update any value with a contentid = 0 to the actual page to which it points.

              From the looks of the data, it looks like the tv values per company id are pointing to different pages, so Smith and Wesson is pointing to 1131. If this is a 1:1 mapping, you might want to look at combining the pages to have the content load more dynamically, which would save you creating more pages (one for each company?). If these were all for one page you could automate this.

              My apologies if I've completely misunderstood what you're trying to get at.
                • 1331
                • 129 Posts
                @stalemate - No, not trying to determine what's in the db. We insert them manually. The idea is to use them as in a TV as tags. Then, to be able to click on the company name to present all posts associated with a given company.

                All of this is working.

                The final touch is to ignore any company names that do not have posts. Coming up with a condition for that is why I'm doing all of this.

                I may have to pull apart the Taglister snippets to see how its done there.
                  • 3749
                  • 24,544 Posts
                  Worst case, you can just test the companies before displaying them:

                  if ($modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvId, 'value' => $companyId)) {
                     /* Company has posts */
                  }
                    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