Hi Bruno,
good to hear from you again. I actually got a few more questions about our custom MIGX configuration. Please let me know if you're available for a little bit of consulting.
Back to this issue though.
I got several relationships between modResources defined with your relations extra.
I'm writing a custom snippet based on your rrgetrelations snippet. Originally i tried to include Target and Source documents into the single graph so i could generate both clinic and doctor output in the same loop and the statement looked like this:
$items = $modx->getCollectionGraph('rrResourceRelation','{"Target":{},"Source":{"TemplateVarResources":{}}}',$criteria);
But that would return nothing, i don't understand why since it looked ok based on the examples. So i removed the Target part which returned Source data but only with one tv for some reason.
So in this case the Target is a clinic and the Source is a doctor. A doctor can be related to multiple clinics and a clinic can have multiple doctors. The clinics are organized in a hierarchy by country and city. So for each country/city i'm trying to list all clinics and the doctors for each clinic. The generated html is for Isotope filtering javascript by clinic.
The full snippet is below. What really bewilders me is that this getCollectionGraph call actually works and returns data for the Source documents and for one tv but not all.
I'm sure there are many ways to do this but i was trying to leverage the graph's promise of returning all related data in a single query. I did try looking at pdoTools but there was no documentation to speak of so I postponed diving into that code to figure it out but from the first glance there was nothing that would return the relationships that the graph provides.
<?php
$location = $modx->getOption('id', $scriptProperties, $modx->resource->get('id'));
$levelType = $modx->getOption('leveltype', $scriptProperties, '');
$outputSeparator = $modx->getOption('outputSeparator', $scriptProperties, ',');
// load migxresourcerelations class
$packageName = 'migxresourcerelations';
$packagepath = $modx->getOption('core_path') . 'components/' . $packageName . '/';
$modelpath = $packagepath . 'model/';
$loaded = $modx->addPackage($packageName, $modelpath, $prefix);
if (! $loaded) {return 'failed to add ' . $packageName;}
$classname = 'rrResourceRelation';
// determine current category and set featured TV
$relName = 'clinics';
$relTV = 67;
// get ids of all sub-areas and their clinics
$areas = $modx->getChildIds($location,5,array('context' => 'web'));
$areas = array_unique($areas);
// get clinic data
$clinics = $clinicIDs = array();
$criteria = $modx->newQuery('modResource');
$criteria->distinct();
$criteria->select('id','pagetitle','alias');
$criteria->where(array('id:IN' => $areas, 'template' => '2'));
$criteria->sortby('pagetitle','ASC');
$collection = $modx->getCollection('modResource', $criteria);
foreach ($collection as $clinic) {
$clinics[$clinic->get('id')]['alias'] = $clinic->get('alias');
$clinics[$clinic->get('id')]['title'] = $clinic->get('pagetitle');
$clinicIDs[] = $clinic->get('id');
}
$clinicIDs = array_unique($clinicIDs);
//get doctor data
$criteria = array();
$criteria['rrResourceRelation.target_id:IN'] = $clinicIDs;
$criteria['TemplateVarResources.tmplvarid:IN'] = array(44,41,40,$relTV);
$criteria = $modx->newQuery('rrResourceRelation', $criteria);
$items = $modx->getCollectionGraph('rrResourceRelation','{"Source":{"TemplateVarResources":{}}}',$criteria);
//this is debug code to figure out why not all tvs are being returned
// the actual code is commented below under 'prepare doctor output'
$target = array();
foreach ($items as $item) {
$target[] = $item->get('source_id');
$target[] = $item->Source->get('pagetitle');
$tvs = array();
$tvCount = 0;
foreach($item->Source->TemplateVarResources as $tvr) {
$tvs[$tvr->get('id')] = $tvr->get('value');
$tvCount++;
}
}
$target[] = $tvs;
$doctorCount = $tvCount;
$clinicOutput = $target;
/*
// prepare doctor output
$featuredCount= 0;
$doctorCount = 0;
$doctorOutput = '';
if (!empty($items)) {
foreach ($items as $item) {
if (is_object($item)) {
$doctorCount++;
$clinicTitle = $item->Target->get('pagetitle');
$clinicAlias = $item->Target->get('alias');
$doctorName = $item->Source->get('pagetitle');
$doctorID = $item->Source->get('id');
$featured = '';
$photo = '';
// get template var values
foreach ($item->Source->getMany('TemplateVarResources') as $tvr) {
switch ($tvr->get('tmplvarid')) {
case 40:
//
$photo = $tvr->get('value');
$photo = (! empty($photo) ? $photo : '[[++theme_base_dir]]images/gallery-img3.jpg');
break;
case $relTV:
// featured_in_...
if ($tvr->get('value') == 1) {
++$featuredCount;
$featured = 'featured';
}
}
}
// generate output
$doctorOutput .= '<article class="entry '.$clinicAlias.' '.$featured.'"><div class="gallery-thumb"><img src="'.$photo.'"></div><p><a href="[[~' .$doctorID. ']]">' . $doctorName . '</a></p></article>';
}
}
}
// prepare clinic filters
$url = $modx->makeUrl($modx->resource->get('id'));
$url = substr($url, 0, -1).'#';
$clinicOutput = '<li><a href="'. $url .'featured" class="selected" data-filter=".featured">Featured</a></li>';
foreach ($clinics as $clinic) {
$clinicOutput .= '<li><a href="'.$url.$clinic['alias'].'" data-filter=".'.$clinic['alias'].'">'.$clinic['title'].'</a></li>';
}
*/
$modx->setPlaceholder('doctorcount', $doctorCount);
$modx->setPlaceholder('filter', $clinicOutput);
$modx->setPlaceholder('doctors', $doctorOutput);
return '';