使用MODx Wayfinder按模板变量值过滤资源 [英] Filter resources by template variable value with MODx Wayfinder

查看:107
本文介绍了使用MODx Wayfinder按模板变量值过滤资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,该网站使用Wayfinder来显示Articles博客中的最​​新3个条目.现在,我只考虑标记为Highlights的博客条目.

I have a site that uses Wayfinder to display the latest 3 entries from an Articles blog. Now, I want to only consider those blog entries that are tagged Highlights.

我最初的Wayfinder呼叫看起来像这样,没什么特别的:

My original Wayfinder call looks like this, nothing spectacular:

[[!Wayfinder? &startId=`296` &level=`1`
    &outerTpl=`emptyTpl`
    &innerTpl=``
    &rowTpl=`thumbnails_formatter`
    &ignoreHidden=`1`
    &sortBy=`menuindex`
    &sortOrder=`DESC`
    &limit=`3`
    &cacheResults=`0`
]]

由于Articles标签是通过articlestags电视管理的,因此我认为&where可能会成功,但还没有运气:

as Articles tags are managed via the articlestags TV, I thought that a &where might do the trick, but with no luck yet:

&where=`[{"articlestags:LIKE":"%Highlights%"}]`

不产生任何结果.作为健全性检查,我尝试了[{"pagetitle:LIKE":"%something%"}],它可以工作.显然,问题在于articlestags不是modx_site_content的列,但是我不确定如何放置子查询.

does not yield anything. As a sanity check, I tried [{"pagetitle:LIKE":"%something%"}], which worked. Obviously, the problem is that articlestags is not a column of modx_site_content, but I'm not sure about how to put the subquery.

SELECT contentid
FROM modx_site_tmplvar_contentvalues
WHERE tmplvarid=17
  AND value LIKE '%Highlights%'

在sql提示符下给我正确的ID,但是像这样将其添加到Wayfinder调用中,结果再次为空:

Gave me the right IDs on the sql prompt, but adding it to the Wayfinder call like this gave an empty result again:

&where=`["id IN (SELECT contentid FROM modx_site_tmplvar_contentvalues WHERE tmplvarid=17 AND value LIKE '%Highlights%')"]`

关于如何实现这一目标的任何想法?我希望与Wayfinder保持一致,但是也欢迎其他解决方案.

Any ideas on how to achieve this? I'd like to stay with Wayfinder for consistency, but other solutions are welcome as well.

推荐答案

经过一番尝试,我找到了解决方案:在引用ID时,我需要包括类名(而不是表名):

A little playing around led me to a solution: I needed to include the class name (not table name) when referring to the ID:

&where=`["modResource.id IN (SELECT contentid FROM modx_site_tmplvar_contentvalues WHERE tmplvarid=17 AND value LIKE '%Highlights%')"]`

一个小的测试表明,即使是简单的

a small test showed that even a simple

&where=`["id = 123"]`

没有modResource.无效.

查看wayfinder.class.php会显示以下行,这似乎是罪魁祸首":

A look at wayfinder.class.php shows the following line, which seems to be the "culprit":

$c->select($this->modx->getSelectColumns('modResource','modResource'));

此方法为所选列取别名-相关代码在xpdoobject.class.php中.第一个参数是类名,第二个是表别名.结果是查询选择id AS modResource.id,依此类推.

This method aliases the selected columns - relevant code is in xpdoobject.class.php. The first parameter is the class name, the second a table alias. The effect is that the query selects id AS modResource.id, and so on.

我的查询的最终版本:

final version of my query:

&where=`["modResource.id IN (
    SELECT val.contentid
    FROM modx_site_tmplvars AS tv
    JOIN modx_site_tmplvar_contentvalues AS val
     ON tv.id = val.tmplvarid
    WHERE tv.name = 'articlestags' AND (
        val.value = 'Highlights'
     OR val.value LIKE 'Highlights,%'
     OR val.value LIKE '%,Highlights'
     OR val.value LIKE '%,Highlights,%'
    )
)"]`

我不认为此查询特别有效(我似乎还记得OR条件不好).此外,如果不删除换行符,则MODx将无法与此程序一起使用.尽管如此,我还是更喜欢以格式正确的形式发布查询.

I don't claim this query is particularly efficient (I seem to recall that OR conditions are bad). Also, MODx won't work with this one if the newlines aren't stripped out. Still, I prefer to publish the query in its well-formatted form.

这篇关于使用MODx Wayfinder按模板变量值过滤资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆