QueryBuilder 原则左连接

问题描述 投票:0回答:1

需要一些帮助才能正确翻译。

旧:

$categories = $GLOBALS["TYPO3_DB"]->exec_SELECTgetRows('c.uid, c.id, c.name AS categoryName, c.tstamp', 'tx_registration_domain_model_product AS p LEFT JOIN tx_registration_domain_model_category AS c ON p.category = c.uid', "p.hidden = 0 AND p.deleted =0 AND c.hidden = 0 AND c.deleted =0 AND c.name != '' AND p.material = '" . $materialUid . "'", 'p.category');

新:

        $catConstraints = [
            $queryBuilder->expr()->neq('c.name', $queryBuilder->createNamedParameter('')),
            $queryBuilder->expr()->eq('p.material', $queryBuilder->createNamedParameter($materialUid, \PDO::PARAM_INT))
        ];

        $categories = $queryBuilder
            ->select('c.uid', 'c.id', 'c.name', 'c.tstamp')
            ->from($productsTable, 'p')
            ->leftJoin(
                'p',
                $categoriesTable,
                'c',
                $queryBuilder->expr()->eq(
                    'p.category',
                    $queryBuilder->quoteIdentifier('c.uid')
                )
            )
            ->where(...$catConstraints)
            ->groupBy('p.category')
            ->executeQuery();

给定的别名“c”在 FROM 和 JOIN 子句表中不是唯一的。目前注册的别名是:tx_registration_domain_model_material, p, c.

doctrine typo3 query-builder
1个回答
1
投票

到目前为止语法看起来是正确的,也可以在这里查找:

https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Database/QueryBuilder/Index.html#join-innerjoin-rightjoin-and-leftjoin

// SELECT `sys_language`.`uid`, `sys_language`.`title`
// FROM `sys_language`
// INNER JOIN `pages` `p`
//     ON `p`.`sys_language_uid` = `sys_language`.`uid`
// WHERE
//     (`p`.`uid` = 42)
//     AND (
//          (`p`.`deleted` = 0)
//          AND (
//                  (`sys_language`.`hidden` = 0)
//              AND (`overlay`.`hidden` =  0)
//          )
//          AND (`p`.`starttime` <= 1475591280)
//          AND (
//                  (`p`.`endtime` = 0)
//               OR (`overlay`.`endtime` > 1475591280))
//     )
$queryBuilder = $this->connectionPool
  ->getQueryBuilderForTable('sys_language');
$result = $queryBuilder
  ->select('sys_language.uid', 'sys_language.title')
  ->from('sys_language')
  ->join(
     'sys_language',
     'pages',
     'p',
     $queryBuilder->expr()->eq(
       'p.sys_language_uid',
       $queryBuilder->quoteIdentifier('sys_language.uid')
     )
  )
  ->where(
    $queryBuilder->expr()->eq(
      'p.uid',
      $queryBuilder->createNamedParameter(42, \PDO::PARAM_INT)
    )
  )
  ->executeQuery();

来自 doctrine/dbal 代码,这也应该适用于您的代码:

  $qb = $conn->createQueryBuilder()
    ->select('u.id', 'p.id')
    ->from('users', 'u')
    ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');

您正在使用两个变量,一个用于

fromTableName ($productsTable)
,一个用于
joinTableName ($categoriesTable)
。阅读错误信息:

给定的别名“c”在 FROM 和 JOIN 子句表中不是唯一的。目前注册的别名是:tx_registration_domain_model_material, p, c.

事实上,只有一个表和两个列出的别名,你要么有:

  • 重复使用相同的 QueryBuilder 实例
    $queryBuilder
    进行第二个查询,以及第二个
    from
    语句(不支持)
  • $productsTable
    $categoriesTable
    或两者都是空字符串

因此请确保您确实为新查询创建了一个新的 QueryBuilder 实例,例如像这样:

$queryBuilder = $connection->createQueryBuilder();
// or $queryBuilder = $connection->getQueryBuilder();
// depending on the TYPO3 version and doctrine/dbal version
$catConstraints = [ /*...*/ ];

并重新检查两个变量的内容。

© www.soinside.com 2019 - 2024. All rights reserved.