TYPO3 Extbase query based on other table

TYPO3 Extbase query based on other table

我到了 table 其中一个与另一个相关

TABLE A
uid
name

TABLE B
uid
name
rel_table_a

现在我想:

// find all table A that is related to by table B
public function findAllTableAThatIsRelatedToByTableB() {
  $query = $this->createQuery();

  // what should this line be?
  //$query->matching($query->count(tableB.rel_table_a = uid));

    return $query->execute();
}

是否可能,或者是否需要首先在 TCA 中为 Table A 创建关系。

您可以在查询中使用相关的 tables,但首先您需要正确设置您的 TCA 和您的模型,否则 extbase 不知道您的 tables是相关的。

作为查询条件,你可以只检查关系是否存在。

在您的结构中,我们只能在 table B 上执行此查询,因为您的 table A 没有关系字段,因此在此示例中,您将从 table B 表示有一个 table A 元素相关。

$query->matching($query->greaterThan('rel_table_a.uid', 0));

如果您有从 table A 到 table B 的 1:n 关系,那么您应该使用内联字段。这样你就可以在 table A 上进行查询:https://docs.typo3.org/typo3cms/TCAReference/Reference/Columns/Inline/Index.html

PS: 你不能在 $query->matching

中使用 $query->count