查询 Join table 按 menudindex 排序

Query Join table to sort by menudindex

这是我的查询:

$results = $modx->query("SELECT contentid FROM modx_site_tmplvar_contentvalues WHERE tmplvarid=107");

我需要按菜单索引对其进行排序,所以我尝试了这个:

$results = $modx->query("SELECT contentid FROM modx_site_tmplvar_contentvalues JOIN modResource WHERE tmplvarid=107 ORDER BY menuindex DESC");

我一点都不熟悉这个。当然不行。有人告诉我使用 XPDO,但我对此一无所知。我想念什么吗?我应该怎么做才能让它发挥作用?

完整代码如下:

<?php
$results = $modx->query("SELECT contentid FROM modx_site_tmplvar_contentvalues WHERE tmplvarid=107");

// description tv: 108
// vignette tv: 121

if (!is_object($results)) {
  return;
}
$breves = array();


while($r = $results->fetch(PDO::FETCH_COLUMN, 0)){
  $breves[] = $r;
}

$queryIn = implode(',' , $breves);

$results_2 = $modx->query("SELECT contentid, tmplvarid, value FROM modx_site_tmplvar_contentvalues WHERE contentid IN ({$queryIn}) AND tmplvarid IN (108, 121)");

if (!is_object($results_2)) {
  return;
}
$tvValues = array();

while($v = $results_2->fetch(PDO::FETCH_ASSOC)){
  if($v['tmplvarid'] == 108){
    $tvValues[$v['contentid']]['desc'] = $v['value'];
  } else {
    $tvValues[$v['contentid']]['vignette'] = $v['value'];
  }
}

$output = "";

foreach ($breves as $res_id) {
  $page = $modx->getObject('modResource', $res_id);
  $alias = $page->get('alias');
  $id = $page->get('id');

  $page_title = $page->get('pagetitle');
  $description = $tvValues[$res_id]['desc'];
  if(!$description || $description == ""){
    $description = $page->get('content');
  }
  $description = strip_tags($description);
  $description = mb_strimwidth($description, 0, 150, "...");
  $vignette = $tvValues[$res_id]['vignette'];
  if($vignette){
    $vignette = "/assets/upload/pln/" . $vignette;
  } else {
    $vignette = "https://www.commune-ploudaniel.fr/assets/templates/pln/default-b7824fcd998f51baf0f0af359a72e760.png";
  }

$output .= <<<HTML
  <div class="xpro-slider-item">
    <div class="xp-news-classic-block">
        <div class="xp-hover-image">
          <a href="{$alias}.html" ><div class="xp-overlay"></div></a>
            <a class="xp-view-lightbox" href="{$alias}.html" ><i class="fa fa-plus"></i></a>
            <img src="{$vignette}" alt="{$alias}" style="width:375px; height:270px; object-fit: cover;"/>
    </div>
        <div class="xp-news-detail">
          <h4><a href="{$alias}.html" data-display="lightbox">{$id}{$page_title}</a></h4>
          <p>{$description}</p>
          <div class='xp-news-footer'></div>
        </div>
    </div>
  </div>
HTML;

$output .= PHP_EOL;
}
return $output;

对 MODX 试试这个:

$q = $modx->newQuery('modTemplateVarResource');
$q->leftJoin('modResource', 'mr', 'mr.id = modTemplateVarResource.contentid');
$q->where(array(
    'modTemplateVarResource.tmplvarid' => 107
));
$q->select(array(
   'modTemplateVarResource.contentid'
));
$q->sortby('modResource.menuindex', 'DESC');
$q->prepare();
$q->stmt->execute();
$result = $q->stmt->fetch(PDO::FETCH_ASSOC);
print_r($result);