简体   繁体   English

TYPO3 selectMultipleSideBySide数据库查询

[英]TYPO3 selectMultipleSideBySide database query

Hi in TYPO3 i have following code嗨,TYPO3 我有以下代码

TCA:三氯乙酸:

'cat' => [
        'exclude' => true,
        'label' => 'LABEL',
        'config' => [
            'type' => 'select',
            'renderType' => 'selectMultipleSideBySide',
...

This saves a string in the table.这会在表中保存一个字符串。 If i select more than one option it saves like this in the table "1,2" (selected categorie 1 and 2).如果我 select 有多个选项,它会像这样保存在表“1,2”(选定的类别 1 和 2)中。 The controller has this code controller 有这个代码

Controller: Controller:

$catlist = explode(',', $categories);
    $searchForValue = ',';
    foreach ($catlist as $cat) {
        $and .= ' OR categories = ' . '"' . $cat . '"';
        if (strpos($categories, $searchForValue) !== false) {
            $where = ' WHERE categories = ' . '"' . $categories .'"' . $and;
        } else {
            $where = ' WHERE categories = ' . '"' . $categories .'"';
        }
    }

this is giving me following statement:这是给我以下声明:

SELECT * FROM TABLE WHERE categories = "2,3" OR categories = "2" OR categories = "3"

But if i have an article with the categories 2 and 1 there ist the string "2,1" in the database and my query wont work.但是,如果我有一篇包含类别 2 和 1 的文章,则数据库中存在字符串“2,1”,我的查询将无法正常工作。 I just get the article with category 3 and not the one with 2 and 1 (2,1).我只得到类别 3 的文章,而不是类别 2 和 1 (2,1) 的文章。 How can i generate a useful query?我如何生成有用的查询?

When I have this type of problem I generally execute a query with a where clause that covers all the possible cases, that are 4 (related to the data stored in the DB)当我遇到这种类型的问题时,我通常会使用包含所有可能情况的 where 子句执行查询,即 4(与存储在数据库中的数据相关)

  • 2 (2 is the only category of the article) 2(2是文章的唯一分类)
  • 2, (2 is the first category of the article) 2、(2是文章的第一类)
  • ,2 (2 is the last category of the article) ,2(2是文章的最后一类)
  • ,2, (2 is one category among others) ,2,(2是其中的一个类别)

Then you have to change the controller that builds the query, so that the WHERE is in this form:然后,您必须更改构建查询的 controller,以便 WHERE 为以下形式:

WHERE categories='2' OR categories LIKE '2,%' OR categories LIKE '%,2' OR categories LIKE '%,2,%'

Surely there is a better and more efficient way, let's see if someone else responds so we learn it.肯定有更好更有效的方法,让我们看看是否有其他人响应,以便我们学习它。

Obviously this solves the problem when the search is done for a single category (I did not understand if your problem is this or that of searching with 2 or more categories).显然,这解决了针对单个类别进行搜索时的问题(我不明白您的问题是这个问题还是搜索 2 个或更多类别的问题)。 In the latter case you have to include all possible combinations in the where, and this greatly increases the complexity of the problem, making my solution very unsuitable.在后一种情况下你必须在where中包括所有可能的组合,这大大增加了问题的复杂性,使我的解决方案非常不合适。

However, if your search on multiple categories is not exclusive, ie it is enough that an article has one of the selected categories (only 2 or 3) then you can simply build the 4 combinations in OR for each category searched.但是,如果您对多个类别的搜索不是排他性的,即一篇文章有一个选定的类别(只有 2 或 3 个)就足够了,那么您可以简单地在 OR 中为搜索的每个类别构建 4 种组合。 This means that with 2 categories you will have 8 where clauses in OR, with 3 categories 12, with N you will have n * 4 where clauses.这意味着对于 2 个类别,您将在 OR 中有 8 个 where 子句,对于 3 个类别,12 个,对于 N,您将有 n * 4 个 where 子句。 As mentioned, you can certainly work on something more performing, but fortunately I have never needed it to date如前所述,你当然可以做一些性能更好的东西,但幸运的是,到目前为止我从来不需要它

EDIT: thank to @GeorgRinger for the tips.编辑:感谢@GeorgRinger提供的提示。 Using the function FIND_IN_SET the construction of the where clause should be something like this (always if only one category match is needed)使用 function FIND_IN_SET where 子句的构造应该是这样的(总是如果只需要一个类别匹配)

$where = "";
foreach ($catlist as $cat) {
    if ($where!=""){
        $where = $where . " OR ";
    }
    $where = $where . " FIND_IN_SET(\"".$cat."\",categories)>0 "
}
$where = "WHERE " . $where

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

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