We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22385
    • 17 Posts
    I wanna merge 3 TVs so I need to copy 2 different TVs into 1 TV before eliminating them. Every resource has either TV1 or TV2 but not both. I just need to this once.
    Any idea how can I do it?
    Many thanks!
      • 3749
      • 24,544 Posts
      This is untested, but should be close. Put this tag where you want the output:

      [!Merge? &tv1 = `[*tv1*]` &tv2 = `[*tv2*]` &tv3 = `[*tv3*]` !]


      Here’s the snippet:

      <?php
      /* Merge snippet */
      
      $output = empty($tv1)? '' : $tv1;
      $output .= empty($tv2)? '' : $tv2;
      $output .= $tv3;
      
      return $output;
      ?>
      
      


      On second thought, you can probably use this as the whole snippet (assuming that I’m understanding what you’re trying to do):
      <?php 
      /* Merge snippet */
      return $tv1 . $tv2 . $tv3;
      ?>

        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
        • 29019 ☆ A M B ☆
        • 104 Posts
        It looks like osanchez is asking for a more permanent solution. If you aren’t, please ignore this post and go with Bob’s more sensible one. If you are, you might try using a SQL query to your [prefix]site_tmplvar_contentvalues table:

        insert into [prefix]site_tmplvar_contentvalues (tmplvarid, contentid, value) select '[new_tv_id]', t1.contentid, concat(t1.value, 'separator', t2.value) from [prefix]site_tmplvar_contentvalues t1, [prefix]site_tmplvar_contentvalues t2 where t1.contentid = t2.contentid and t1.tmplvarid = [first_tv_id] and t2.tmplvarid = [second_tv_id];


        Replace [prefix] with your table prefix (default is ’modx_’), [new_tv_id] with the id of the TV you’d like to merge to, [first_tv_id] with the first TV id you’d like to merge, ’separator’ with whatever you want between the two tvs (you can omit this if you want) and [second_tv_id] with the second TV id you’d like to merge.

        After running this, I’d delete your excess template variables through the manager.

        Good luck!
          www.darkstardesign.com

          Are you in the Nashville area? Join us for our next MODX meetup!
          • 22385
          • 17 Posts
          ERROR
            • 22385
            • 17 Posts
            Thanks both.

            Bob’s answer is not quite complete because I should place that snippet in every page/resource.

            And swespi answer is very close but I think "INSERT INTO" is not the solution. This 3rd variable already exists, so I just need to place the TV1 and TV2 value to the TV3. It doesn’t have to be done both together. I can first copy TV1 to TV3 and then TV2 to TV3.

            Again the problem is to copy the value of 2 custom TVs to a modx default variable (summary). The three of them are text variables.
            Once done, I’d delete the custom variables (TV1 & TV2).

            Many thanks guys!!
              • 29019 ☆ A M B ☆
              • 104 Posts
              The new template variable may already exist, but do you already have entries in the [prefix]site_tmplvar_contentvalues table for it? That insert isn’t inserting new template variables, just new values for an existing template variable (with the given id).

              Just to clarify: I’m assuming that you’ve already created this template variable, but every resource that has the other two TVs doesn’t have a value for the new one, and needs to have those two values combined.

              If there are any resources that are already using this TV and the other two, you will end up with some duplicate values in your table for the same resource/tv combination - that would be a very bad thing. I would make sure to flush these values before you do the insert:

              select t3.*
              from 
              	[prefix]site_tmplvar_contentvalues t1
              	, [prefix]site_tmplvar_contentvalues t2
              	, [prefix]site_tmplvar_contentvalues t3 
              where 
              	t1.contentid = t2.contentid 
              	and t3.contentid = t2.contentid 
              	and t3.tmplvarid = [new_tv_id] 
              	and t1.tmplvarid = [first_old_tv_id] 
              	and t2.tmplvarid = [second_old_tv_id]


              That should give you a good idea about existing TV values you might want to delete. Please make a backup of your database before you try any of these. My examples are certainly not typo-proof. wink


              EDIT:

              Again the problem is to copy the value of 2 custom TVs to a modx default variable (summary).

              I just noticed this bit which really changes the problem quite a bit - sorry! I should have read that a little bit more closely. Here’s a very messy, very slow modification that *should* do the trick, but I’m really just doing this off the top of my head, so again be sure to make a backup:

              update [prefix]site_content set introtext = (
              	select concat(t1.value, 'separator', t2.value) 
              	from 
              		[prefix]site_tmplvar_contentvalues t1
              		, [prefix]site_tmplvar_contentvalues t2 
              	where 
              		t1.contentid = t2.contentid 
              		and t1.contentid = [prefix]site_content.id 
              		and t1.tmplvarid = [first_old_tv_id] 
              		and t2.tmplvarid = [second_old_tv_id]
              ) where id in (
              	select [prefix]site_content.id 
              	from [prefix]site_tmplvar_templates 
              	where 
              		[prefix]site_content.template = [prefix]site_tmplvar_templates.templateid 
              		and [prefix]site_tmplvar_templates.tmplvarid in ([first_old_tv_id], [second_old_tv_id])
              );


                www.darkstardesign.com

                Are you in the Nashville area? Join us for our next MODX meetup!
                • 22385
                • 17 Posts
                Thanks swespi.

                The snippet doesn’t return any error but when I go and check the resources/pages the default modx TV (summary) is still empty.

                In the meanwhile, there’s any way to navigate through tables on ModX? I don’t have access to the server nor any phpMyAdmin installed.

                Thanks swespi and everybody else following this post.
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  If your server allows remote access to the database, any desktop MySQL client will do the trick. Just Google for one; there’s a bunch of them. You might even be able to use phpMyAdmin from your local machine if you have a development web environment like XAMPP on it. All you need is the server name (or IP address) and login details.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 26931
                    • 2,314 Posts
                    In the meanwhile, there’s any way to navigate through tables on ModX? I don’t have access to the server nor any phpMyAdmin installed.
                    not sure, maybe this would work "MODxBuddy, SQL Buddy as a module for MODx" http://modxcms.com/forums/index.php?topic=46028.0
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Or if you have SSH access, there’s always the command line that’s lots of fun tongue
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org