获取ManyToMany关系-Symfony\ApiPlatform



在这里描述您的问题或您正在尝试做什么。

你好,

我正在进行一个小型Symfony/ApiPlatform项目,但我必须承认,我真的陷入了一个(小?(问题。。

我有几个实体,包括@City实体、@Company实体和@Manager实体。每个城市可以有多家公司,每个公司可以有多个城市;每个公司可以有多个经理,但每个经理只能有一个公司。

因此,经理必须与一家公司联系在一起,也必须与一个城市联系在一起。例如:

MyCompany Limited在纽约和洛杉矶设有办事处,我们在纽约有一名经理,在洛杉矶有另一名经理。到目前为止,一切都很简单。但事情变得棘手的是,我不能把经理限制在公司所在的城市。例如,MyCompagny Limited的纽约经理不应该将他的城市改为芝加哥,因为他的公司不在这个城市。

我试图从PRE_VALIDATE中的事件中恢复,我设法恢复了经理的公司,但另一方面,$company->getCity()方法返回了我的@company对象,而不是城市对象?

class ManagerCitySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['setCityForManager', EventPriorities::PRE_VALIDATE]
];
}
public function setCityForManager(ViewEvent $event)
{
$result = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if ($result instanceof Manager && ($method === "POST" || $method === "PATCH"))
{
$company= $result->getCompany();
$city= $result->getCity();
$cityCompany = $company->getCity();
if($city instanceof City){
dd(
$company, // dd : return my company object
$city,     // dd : returns the city the user wants
$cityCompany // dd : return also my company object ?
);
} else {
dd("This is not an instance of @City ; else");
}
};
}
}

我想要什么

很简单,在这个阶段,用户(经理(可以为自己分配他想要的城市,比如芝加哥或波士顿(前提是他知道他的IRI(,即使他的公司不在这个城市,只在纽约和洛杉矶。因此,我想将这种可能性限制在与他的公司相同的城市。同一家公司的经理可以改变城市,只要他尊重公司的一个城市。所以我试图找回他的公司,他想给自己分配的城市,以及他公司的城市,以便能够提出一个条件:

if($desiredcity == $cityOfMyCompany) { then I assign the city; } else { I return an error: You don't have access to this city; }

我得到的

我不能满足上述条件,因为我无法检索经理所在公司的城市。然而,我设法得到了经理的公司,他想分配给自己的城市,但我的$公司->getCity((方法返回我的公司对象:

dd($cityCompany); =
ManagerCitySubscriber.php on line 35:
DoctrineORMPersistentCollection {#1049
#collection: DoctrineCommonCollectionsArrayCollection {#1051
-elements: []
}
#initialized: false
-snapshot: []
-owner: Proxies__CG__AppEntityCompany {#797
-id: 3
-createdAt: DateTimeImmutable @1662842908 {#1052
date: 2022-09-10 20:48:28.0 UTC (+00:00)
}
-company: "MyCompany Limited"
-companyState: "Limited"
-city: DoctrineORMPersistentCollection {#1049}
-Manager: DoctrineORMPersistentCollection {#1056
#collection: DoctrineCommonCollectionsArrayCollection {#1057
-elements: []
}
#initialized: false
-snapshot: []
-owner: Proxies__CG__AppEntityCompany {#797 …2}
-association: array:15 [ …15]
-em: DoctrineORMEntityManager {#642 …11}
-backRefFieldName: "company"
-typeClass: DoctrineORMMappingClassMetadata {#692 …}
-isDirty: false
}
-Clients: DoctrineORMPersistentCollection {#1058
#collection: DoctrineCommonCollectionsArrayCollection {#1059
-elements: []
}
#initialized: false
-snapshot: []
-owner: Proxies__CG__AppEntityCompany{#797 …2}
-association: array:15 [ …15]
-em: DoctrineORMEntityManager {#642 …11}
-backRefFieldName: "company"
-typeClass: DoctrineORMMappingClassMetadata {#796 …}
-isDirty: false
}
+__isInitialized__: true
…2
}
-association: array:20 [ …20]
-em: DoctrineORMEntityManager {#642 …11}
-backRefFieldName: "companies"
-typeClass: DoctrineORMMappingClassMetadata {#770 …}
-isDirty: false
}

Re,

我"找到";我的问题的解决方案,但我不知道这是否是正确的方法。如果有人对更干净的代码有更好的想法,那就非常高兴了:

public function setCityForManager(ViewEvent $event)
{
$result = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if ($result instanceof Manager && ($method === "POST" || $method === "PATCH"))
{
$company= $result->getCompany();
$city= $result->getCity();
if($city instanceof City && in_array($city, $company->getCity()->toArray())) {
$result->setCity($city);
} else {
die();
}
};
}

最新更新