简体   繁体   English

ModX Revo:查询多台电视?

[英]ModX Revo: Query Multiple TVs?

I'm moving a site to ModX Revolution and have no experience with xPDO. 我正在将一个网站转移到ModX Revolution并且没有使用xPDO的经验。
The site I'm moving has a search feature that looks through a few TVs assigned to some resources and returns the applicable pages. 我正在移动的网站有一个搜索功能,可以查看分配给某些资源的几台电视并返回适用的页面。
I'm having trouble incorporating this using xPDO. 我在使用xPDO进行此操作时遇到了麻烦。

I'm able to return all pages where a TV is set to a given value, but I can't figure out how to expand this into: 我能够将电视设置为给定值的所有页面都返回,但我无法弄清楚如何将其扩展为:
Find all resources where TV1 == X, TV2 == Y, TV3 == Z . Find all resources where TV1 == X, TV2 == Y, TV3 == Z
How can I query multiple TVs at once? 如何一次查询多台电视?

$value = "Mexico";
$c = $modx->newQuery('modResource');
$c->innerJoin('modTemplateVarResource','TemplateVarResources');
$c->where(array(
     'TemplateVarResources.tmplvarid' => 7,
     '"'.$value.'" IN (TemplateVarResources.value)',
));

$resources = $modx->getCollection('modResource',$c);

Hmmmmm I think it goes something like this: 嗯,我觉得它是这样的:

$c->where(array(
     'TV1:=' => 'X'
     'AND:TV2:=' => 'Y'
     'AND:TV3:=' => 'Z'
));

Would be good to check the docs here: http://rtfm.modx.com/xpdo/2.x/class-reference/xpdoquery 最好查看这里的文档: http//rtfm.modx.com/xpdo/2.x/class-reference/xpdoquery

Working example from my code for two tvs: reserved (id:11) and cost_obj (id:12) 我的代码中的两个电视的工作示例: reserved (id:11)和cost_obj (id:12)

$c->leftJoin('modTemplateVarResource', 'cost_obj', array('modResource.id = cost_obj.contentid', 'cost_obj.tmplvarid = 12'));
$c->leftJoin('modTemplateVarResource', 'reserved', array('modResource.id = reserved.contentid', 'reserved.tmplvarid = 11'));
$c->query['where'][0][0][] = new xPDOQueryCondition(array(
   'sql' => 'CAST(`reserved`.`value` as UNSIGNED INTEGER) > ?',
   'binding' => array(
                     'value' => 0,
                     'type' => PDO::PARAM_INT,
                     'length'=>0),
   'conjunction'=>'AND')
);
$c->query['where'][0][1][] = new xPDOQueryCondition(array(
   'sql' => 'CAST(`cost_obj`.`value` as UNSIGNED INTEGER) > ?',
   'binding' => array(
                     'value' => 0,
                     'type' => PDO::PARAM_INT,
                     'length'=>0),
   'conjunction'=>'AND')
);
$c->query['where'][0][2][] = new xPDOQueryCondition(array(
   'sql' => 'CAST(`reserved`.`value` as UNSIGNED INTEGER) >= CAST(`cost_obj`.`value` as UNSIGNED INTEGER)',
   'conjunction'=>'AND')
);

this query equal 这个查询相等

reserved > 0 AND cost_obj > 0 AND reserved >= cost_obj

Simply innerJoin a modTemplateVar table for each TV you want to search on. 只需内部加入一个modTemplateVar表,用于您要搜索的每台电视。 Note the third param in the join which contains the ON terms. 注意连接中包含ON术语的第三个参数。 Make sure you specify the correct TV id's you want to get. 确保指定了您想要的正确电视ID。

$value = "Mexico";
$c = $modx->newQuery('modResource');
$c->innerJoin('modTemplateVar', 'TV1', 'TV1.contentid = modResource.id AND TV1.tmplvarid = X');
$c->innerJoin('modTemplateVar', 'TV2', 'TV2.contentid = modResource.id AND TV2.tmplvarid = Y');
$c->innerJoin('modTemplateVar', 'TV3', 'TV3.contentid = modResource.id AND TV3.tmplvarid = Z');
$c->where(array(
   'TV1.value:LIKE' => $value,
   'TV2.value'...,
   'TV3.value'...
));

$resources = $modx->getCollection('modResource',$c);

Please note that this is untested but it should give you the general idea of how you should set up your query. 请注意,这是未经测试的,但它应该让您大致了解如何设置查询。 You may need to add some backticks to the table names in the ON clauses, dunno how picky the xPDO parser is about that. 你可能需要在ON子句中的表名中添加一些反引号,不管xPDO解析器是多么挑剔。

Also note that this isn't a very effective query on larger sets. 另请注意,对于较大的集合,这不是一个非常有效的查询。 The most effective way of accomplishing this would be to make a custom xpdo object that suits your needs, but thats quite a project if you only need it for this specific case. 实现这一目标的最有效方法是制作一个适合您需求的自定义xpdo对象,但如果您只针对这种特定情况需要它,那就是一个相当大的项目。

If possible you might also just try doing a getResources call, where it's fairly easy to add TV contitionals. 如果可能的话,您也可以尝试进行getResources调用,这样可以很容易地添加电视转换。 It's still slow since it's done with more or less the same joins but atleast you won't have to troubleshoot your way to the solution. 它仍然很慢,因为它完成了或多或少相同的连接,但至少你不必解决你的解决方案。

[[getResources?
    &parents=`1,2,3...`
    &depth=`10` 
    &tpl=`yourTemplateForEachResource`
    &tvFilters=`nameOfTV1==%[[+value]]%,othertv==othervalue`
]]

Note that comma is AND's and double pipes are OR's in the tvFilters string. 请注意,逗号是AND,双管道是tvFilters字符串中的OR。 And doing it programatically: 并以编程方式执行:

$value = 'Mexico';
$resources = $modx->runSnippet('getResources', array(
    'parents' => '1,2,3...',
    'depth' => 10,
    'tpl' => 'yourTemplateForEachResource',
    'tvFilters' => 'nameOfTV1==%' . $value . '%,othertv==othervalue'
));

I guess building your own query would usually be faster but there's some caching and optimizing built into getResources that might win in the long run (if used correctly). 我想构建你自己的查询通常会更快但是内置到getResources中的一些缓存和优化可能会在长期内获胜(如果使用得当)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM