嗨,我在两个表,联系人和标签之间有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
它抛出实体应该具有主键的错误。
知道吗?
要做左连接:
$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 ZendSqlSelect::JOIN_LEFT