我的数据库模型如下所示:
一个用户可以管理多个公司(USER n:n COMPANY),假设它们在中间表 USER_COMPANY 中连接,并且该表有一个附加字段“活动”。
我为两个表创建了 EntityDefinitions 以及为中间表创建了 MappingDefinition:
公司:
class CompanyDefinition extends EntityDefinition
{
...
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
...
new ManyToManyAssociationField(
'users',
UserDefinition::class,
CompanyUserDefinition::class,
'company_id',
'user_id'
),
]);
}
}
用户:
class UserDefinition extends EntityDefinition
{
....
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
...
new ManyToManyAssociationField(
'companies',
CompanyDefinition::class,
CompanyUserDefinition::class,
'user_id',
'company_id'
),
]);
}
}
公司_用户:
class CompanyUserDefinition extends EntityDefinition
{
...
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new FkField('company_id', 'companyId', CompanyDefinition::class))->addFlags(new PrimaryKey(), new Required()),
(new FkField('user_id', 'userId', UserDefinition::class))->addFlags(new PrimaryKey(), new Required()),
(new BoolField('active', 'active'))->addFlags(new Required()),
new ManyToOneAssociationField('user', 'user_id', UserDefinition::class, 'id'),
new ManyToOneAssociationField('company', 'company_id', CompanyDefinition::class, 'id'),
]);
}
}
现在我想使用 CompanyRepository 进行过滤。我只想检索连接到登录用户的公司(我知道他们的 ID),而连接需要处于活动状态。有什么办法可以做到这一点?这不起作用:
$criteria = new Criteria();
$criteria->addAssociation('users');
$criteria->addFilter(new EqualsFilter('users.active', 1));
$companies = $this->companyRepository->search($criteria, $context);
它显示了这个错误:
"class": "Shopware\\Core\\Framework\\DataAbstractionLayer\\Dbal\\Exception\\UnmappedFieldException",
"message": "Field \"active\" in entity \"user\" was not found.",