<![CDATA[ How to extract diferent tpl variables for 1 resource? - MODX Community Forums]]> https://forums.modx.com/thread/?thread=104598 <![CDATA[How to extract diferent tpl variables for 1 resource?]]> https://forums.modx.com/thread/104598/how-to-extract-diferent-tpl-variables-for-1-resource#dis-post-562568
How can i query the DB in order to extract multiple tpl variables for 1 resource?

i mean, as you already know, THE TABLE modx_site_content contains pagetitle and content in this table.

THE TABLE modx_site_tmplvar_contentvalues contains the tplvars a user created for different purposes: it contains as columns contentid(which refers to the id in modx_site_content) and value(which contains the value of, for example, image-description, image and SEO-Title)

modx_site_content
-----------------------------------------------------------
id | pagetitle | content
-----------------------------------------------------------
1 article 1 this is the content1...
2 article 2 this is the content2...


modx_site_tmplvar_contentvalues
-------------------------------------------------------------
tmplvarid | contentid | value
-------------------------------------------------------------
1 1 path-to-image1.jpg
2 1 description for image1
3 1 SEO title for article 1
1 2 path-to-image2.jpg
2 2 description for image2
3 2 SEO title for article 2

i would like this result:

pagetitle | content | image(it contains the url) | image-description | SEO-Title
----------------------------------------------------------------------------------------------------------------
article 1 | this is the content1... | path-to-image.jpg | description for image1 | SEO title for article 1
article 2 | this is the content2... | path-to-image2.jpg | description for image2 | SEO title for article 2


Thanks and would appreciate your help
sorry, file attachment(table-ok.jpg) ifs for the table(screenshot)]]>
jacielluve Nov 06, 2018, 05:56 PM https://forums.modx.com/thread/104598/how-to-extract-diferent-tpl-variables-for-1-resource#dis-post-562568
<![CDATA[Re: How to extract diferent tpl variables for 1 resource?]]> https://forums.modx.com/thread/104598/how-to-extract-diferent-tpl-variables-for-1-resource#dis-post-562602 jacielluve Nov 07, 2018, 03:34 PM https://forums.modx.com/thread/104598/how-to-extract-diferent-tpl-variables-for-1-resource#dis-post-562602 <![CDATA[Re: How to extract diferent tpl variables for 1 resource? (Best Answer)]]> https://forums.modx.com/thread/104598/how-to-extract-diferent-tpl-variables-for-1-resource#dis-post-562575
In a snippet, you could use $modx->getCollectionGraph() to get the resources and their TV values in one query *if* no TV is set to its default value (I think you're fine on that).

The simplest method (slower, but not as slow as getResources) is to get the resources with getCollection(), then do something like this:

$output = '';
$docs = $modx->getCollection('modResource', $criteria);
foreach($docs as $doc) {
   $fields = array(
       'pagetitle' => $doc->get('pagetitle'),
       'image' => $doc->getTVValue('image'), // name or ID of the TV in the parentheses
       'description' => $doc->getTVValue('description'),
   );
   $output .= $modx->getChunk('myTpl', $fields);
}
return $output;

The myTpl Tpl chunk would look like this:

<div class="item">
    <p>[[+pagetitle]]</p>
    <img src="[[+image]]">
    <p>[[+description]]</p>
</div>




]]>
BobRay Nov 06, 2018, 09:47 PM https://forums.modx.com/thread/104598/how-to-extract-diferent-tpl-variables-for-1-resource#dis-post-562575