如何计算父资源中日期电视不为空的资源数量?

How to count number of resources with date TV is-not-empty in a parent resource?

案例:我需要计算父资源($parentID)中的资源(或电视数量)数量,其中电视($tvID)不为空。

我可以使用此代码

$total = $modx->getCount('modTemplateVarResource', array('tmplvarid' => $tvID, 'value' != $value));
return $total;

和片段调用:

[[!tvValueCount? &value=`` &tvID=`1`]]

但它在 all 资源中计算 all 具有该 ID 的非空电视,我需要将其减少到,让比如说,$parentID。所以基本上我需要这样的片段调用:

[[!tvValueCount? &value=`` &tvID=`xx` &parentID=`xx`]]

我希望它非常简单,但我不是一个足够好的程序员,无法根据数据库负载、处理时间、服务器负载等以最佳方式完成它。

最多将有大约 3-4000 个资源可供统计。

你可以使用这样的东西:

<?php
$c = $modx->newQuery('modTemplateVarResource');
$c->leftJoin('modResource', 'Resource', array(
    'Resource.id = modTemplateVarResource.contentid'
));
$c->where(array(
    'modTemplateVarResource.tmplvarid' => $tvID,
    'modTemplateVarResource.value:!=' => '',
    'Resource.parent' => $parentID,
));
return $modx->getCount('modTemplateVarResource', $c);
return $total;

或者从另一边加入表格:

<?php
$c = $modx->newQuery('modResource');
$c->leftJoin('modTemplateVarResource', 'TemplateVarResources', array(
    'Resource.id = modTemplateVarResource.contentid'
));
$c->where(array(
    'modTemplateVarResource.tmplvarid' => $tvID,
    'modTemplateVarResource.value:!=' => '',
    'Resource.parent' => $parentID,
));
return $modx->getCount('modResource', $c);
return $total;

这将计算所有具有父 $parentID(仅一级)的资源,其中模板变量填充了一个非空字符串。它不计算使用默认模板变量值的资源。

必须扩展代码以查询 deleted/unpublished 资源,计算不止一个级别的资源等。

但在尝试扩展代码之前,您最好使用 getResources 或 pdoResources 片段调用。它们是为此而构建的,您不必为上述例外情况而烦恼。他们用找到的资源的数量填充一个占位符。

这应该与 getResources 一起使用:

[[getResources?
&parents=`parents`
&includeTVs=`tvName`
&tvFilters=`tvName!=`
&totalVar=`totalnotempty`
]]
[[+totalnotempty]]

parentstvName 必须替换为实际值。