簡體   English   中英

多對多關系的吸氣劑 - Symfony\ApiPlatform

[英]Getter on a ManyToMany relation - Symfony\ApiPlatform

在這里描述您的問題或您正在嘗試做的事情。

你好,

我正在進行一個小型 Symfony/ApiPlatform 項目,但我不得不承認我真的陷入了一個(小?)問題。

我有幾個實體,包括@City 實體、@Company 實體和@Manager 實體。 每個城市可以有多個公司,每個公司可以有多個城市; 每個公司可以有多個經理,但每個經理只能有一個公司。

因此,經理必須與公司聯系起來,但也必須與城市聯系起來。 例如:

MyCompany Limited 位於紐約和洛杉磯,我們在紐約和洛杉磯分別有一名經理。 到目前為止,這非常簡單。 但事情變得棘手的是,我不能將經理僅限於公司城市。 例如,MyCompagny Limited 的紐約經理不應該能夠將他的城市更改為芝加哥,因為他的公司不在這個城市。

我嘗試從我在 PRE_VALIDATE 中的事件中做到這一點,我設法恢復經理的公司,但另一方面$company->getCity()方法返回我的@Company object 而不是城市 object?

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),即使他的公司不在這個城市而只在 New約克和洛杉磯。 因此,我想將這種可能性限制在與他公司相同的城市。 同一家公司的經理可以換城市,只要他尊重他公司的一個城市。 所以我正在嘗試檢索他的公司,他想要分配給自己的城市,以及他公司的城市,以便能夠提出條件:

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

我得到了什么

我無法執行上述條件,因為我無法檢索經理公司的城市。 但是,我設法獲得了經理的公司,即他想要分配給自己的城市,但我的 $company->getCity() 方法返回了我的公司 object:

dd($cityCompany); =

ManagerCitySubscriber.php on line 35:
Doctrine\ORM\PersistentCollection {#1049
  #collection: Doctrine\Common\Collections\ArrayCollection {#1051
    -elements: []
  }
  #initialized: false
  -snapshot: []
  -owner: Proxies\__CG__\App\Entity\Company {#797
    -id: 3
    -createdAt: DateTimeImmutable @1662842908 {#1052
      date: 2022-09-10 20:48:28.0 UTC (+00:00)
    }
    -company: "MyCompany Limited"
    -companyState: "Limited"
    -city: Doctrine\ORM\PersistentCollection {#1049}
    -Manager: Doctrine\ORM\PersistentCollection {#1056
      #collection: Doctrine\Common\Collections\ArrayCollection {#1057
        -elements: []
      }
      #initialized: false
      -snapshot: []
      -owner: Proxies\__CG__\App\Entity\Company {#797 …2}
      -association: array:15 [ …15]
      -em: Doctrine\ORM\EntityManager {#642 …11}
      -backRefFieldName: "company"
      -typeClass: Doctrine\ORM\Mapping\ClassMetadata {#692 …}
      -isDirty: false
    }
    -Clients: Doctrine\ORM\PersistentCollection {#1058
      #collection: Doctrine\Common\Collections\ArrayCollection {#1059
        -elements: []
      }
      #initialized: false
      -snapshot: []
      -owner: Proxies\__CG__\App\Entity\Company{#797 …2}
      -association: array:15 [ …15]
      -em: Doctrine\ORM\EntityManager {#642 …11}
      -backRefFieldName: "company"
      -typeClass: Doctrine\ORM\Mapping\ClassMetadata {#796 …}
      -isDirty: false
    }
    +__isInitialized__: true
     …2
  }
  -association: array:20 [ …20]
  -em: Doctrine\ORM\EntityManager {#642 …11}
  -backRefFieldName: "companies"
  -typeClass: Doctrine\ORM\Mapping\ClassMetadata {#770 …}
  -isDirty: false
}

回覆,

我“找到”了我的問題的解決方案,但是我不知道這是否是正確的方法。 如果有人對更簡潔的代碼有更好的想法,我很高興:

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){
                if(in_array($city, $company->getCity()->toArray())) {
                    dd("Yes, it's the same city");
                } else {
                    dd("No, it's not the same city");
                }
            } else {
                dd("This is not an instance of @City ; else");
            }
        };
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM