问题定义
因此,考虑到Doctrine文档的这一部分(以及许多其他关于双向关联和持久性问题的文章(,我有一个单向的一对多关联。它位于Club
和它的Staff
成员之间。根据域逻辑,Club
应该拥有Staff
。例如,如果我使用文档存储,Staff
将是Club
的成员。在99.9%的域案例中,我有一个club
,并且想要它的工作人员列表。
然而,我很难在教义中实现这一理念。因为在条令中,"一对多"关系只有相反的一面。因此,即使在概念上,下面的实体定义也是错误的,但我不知道有什么更好的定义。
实体代码
<?php
class Club
{
/**
* @ORMId
* @ORMColumn(type="uuid", unique=true)
* @ORMGeneratedValue(strategy="CUSTOM")
* @ORMCustomIdGenerator(class="RamseyUuidDoctrineUuidGenerator")
*/
public ?UuidInterface $id;
/**
* @ORMColumn(type="string", name="name")
*/
public string $name;
/**
* @ORMOneToMany(targetEntity="Staff", mappedBy="club")
*/
public Collection $staff;
public function __construct(string $name)
{
$this->id = Uuid::uuid4();
$this->name = $name;
$this->staff = new ArrayCollection();
}
public function addStaff(Staff $staff): void
{
$this->staff->add($staff);
}
}
class Staff
{
public const ROLE_OWNER = 'owner';
/**
* @ORMId
* @ORMColumn(type="uuid", unique=true)
* @ORMGeneratedValue(strategy="CUSTOM")
* @ORMCustomIdGenerator(class="RamseyUuidDoctrineUuidGenerator")
*/
public ?UuidInterface $id;
/**
* @ORMColumn(type="string", name="user_id")
*/
public string $userId;
/**
* @ORMColumn(type="string", name="role")
*/
public string $role;
public function __construct(string $userId, string $role)
{
$this->id = Uuid::uuid4();
$this->userId = $userId;
$this->role = $role;
}
}
但真正的问题来了。我有一个用例,我想为合适的员工找到俱乐部。这是一个(一-(多-一问题,需要注意的是,第一个一个不是另一个条令实体,而是Staff
实体的userId
。
因此,我尝试了几种方法来获得给定userId
:的Club
实体
失败的尝试
<?php
// 1. Naive solution, just try to use the `ObjectRepository`
// Will cause: You cannot search for the association field 'Club#staff', because it is the inverse side of an association. Find methods only work on owning side associations.
$staffers = $this->em->getRepository(Staff::class)->findBy(['userId' => $ownerId]);
$clubs = $this->em->getRepository(Club::class)->findBy(['staff' => $staffers]);
// 2. Use `QueryBuilder` naively
// Will cause: [Semantical Error] line 0, col 131 near 'staff = s.id': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected
$qb = $this->em->createQueryBuilder();
$query = $qb->select('c')
->from(Club::class, 'c')
->join(Staff::class, 's', Join::WITH, 'c.staff = s.id')
->where('s.userId = :id')
->setParameter('id', $ownerId)
->getQuery();
// 3. Use `QueryBuilder` with knowledge if actual DB columns
// Will cause: [Semantical Error] line 0, col 138 near 'club_id WHERE': Error: Class Staff has no field or association named club_id
$qb = $this->em->createQueryBuilder();
$query = $qb->select('c')
->from(Club::class, 'c')
->join(Staff::class, 's', Join::WITH, 'c.id = s.club_id')
->where('s.userId = :id')
->setParameter('id', $ownerId)
->getQuery();
// 4. Create query directly
// Will cause: [Semantical Error] line 0, col 125 near 'staff = s.id': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected
$query = $this->em->createQuery('SELECT c FROM ' . Club::class . ' c JOIN ' . Staff::class . ' s WITH c.staff = s.id WHERE s.userId = :id')->setParameter('id', $ownerId);
我在找什么
Club
和Staff
之间的单向关联,这样我就不必担心持久性、性能、不一致性等。只是双向关联的所有问题- 或者:
- 实体/关联的可能返工
- 在给定
staff.userId
的情况下检索Club
实体的方法
一个正在运行的解决方案,但我无法解释为什么这个解决方案有效,而其他解决方案无效。
<?php
$this->em->createQueryBuilder()
->select('c')
->from(Club::class, 'c')
->innerJoin(Staff::class, 's')
->where('s.userId = :owner')
->setParameter('owner', $ownerId)
->getQuery()
->getResult();