如何在学说zend中没有实体的情况下在查询生成器中保留联接

时间:2019-01-31 11:19:05

标签: zend-framework doctrine-orm doctrine left-join zend-framework3

嗨,我在两个表Contacts和Tag之间有M:M关系,它们的M:M表称为Contacts_Tags:

Contacts
------------
ID
Name

Tags
-----------
ID
Name

Contacts_Tags
--------------
Contact_ID
Tag_ID

我有名为Contact的联系人实体和名为Tag的标签实体,但没有Contacts_Tags表的实体。

我想退出查询构建器

$queryBuilder                    =  $this->entityManager->getRepository(Contact::class)->createQueryBuilder("o")->select("o");
$queryBuilder->leftJoin(//here, "et", "WITH", "et.Contact_ID = o.ID")
             ->leftJoin(Tag::class, "t", "WITH", "t.ID = et.Tag_ID")
;

但是我不知道如何添加它。我尝试了文档,但是它说当我添加ContactTag的实体时添加Entity会引发错误,提示该实体应具有主键。

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

要做左加入:

$queryBuilder->join(table, condition, columns, $queryBuilder::JOIN_LEFT);
with :
table is the name of a table or another Select or array [alias => table]
condition is a string (the same as in Sql language)
columns is an array like [alias => column_name, ...] or [column_name, ...], may be empty
$queryBuilder is a Select, can be replaced by \Zend\Sql\Select::JOIN_LEFT