简体   繁体   English

多对多关系的吸气剂 - Symfony\ApiPlatform

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

Describe here your problem or what you are trying to do.在这里描述您的问题或您正在尝试做的事情。

Hello,你好,

I'm on a small Symfony/ApiPlatform project but I have to admit that I'm really stuck on a (small?) problem..我正在进行一个小型 Symfony/ApiPlatform 项目,但我不得不承认我真的陷入了一个(小?)问题。

I have several entities including a @City entity, an @Company entity and an @Manager entity.我有几个实体,包括@City 实体、@Company 实体和@Manager 实体。 Each city can have multiple companies, and each company can have multiple cities;每个城市可以有多个公司,每个公司可以有多个城市; Each company can have multiple managers, but each manager can only have one company.每个公司可以有多个经理,但每个经理只能有一个公司。

So a manager must be linked to a company but also to a city.因此,经理必须与公司联系起来,但也必须与城市联系起来。 For instance:例如:

MyCompany Limited is present in New York and Los Angeles, we have a manager for New York and another manager for Los Angeles. MyCompany Limited 位于纽约和洛杉矶,我们在纽约和洛杉矶分别有一名经理。 So far, it's very simple.到目前为止,这非常简单。 But where things get tricky is that I can't limit a manager to only company cities.但事情变得棘手的是,我不能将经理仅限于公司城市。 For example, the New York manager of MyCompagny Limited should not be able to change his city to Chicago, given that his company is not present in this city.例如,MyCompagny Limited 的纽约经理不应该能够将他的城市更改为芝加哥,因为他的公司不在这个城市。

I try to do it from my events in PRE_VALIDATE, I manage to recover the manager's company but on the other hand the $company->getCity() method returns my @Company object rather than the city object?我尝试从我在 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");
            }
        };
    }
}

What I want我想要的是

It's quite simple, at this stage the user (Manager) can assign himself the city he wants, such as the city of Chicago or Boston (provided he knows his IRI), even if his company is not present in this city but only in New York and Los Angeles.这很简单,在这个阶段,用户(经理)可以为自己分配他想要的城市,例如芝加哥市或波士顿市(只要他知道他的 IRI),即使他的公司不在这个城市而只在 New约克和洛杉矶。 So I would like to limit the possibility only to the same cities as his company.因此,我想将这种可能性限制在与他公司相同的城市。 A manager of the same company can change city, as long as he respects one of the cities of his company.同一家公司的经理可以换城市,只要他尊重他公司的一个城市。 So I'm trying to retrieve his company, the city he wants to assign himself but also the cities of his company to be able to make a condition:所以我正在尝试检索他的公司,他想要分配给自己的城市,以及他公司的城市,以便能够提出条件:

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

what i get我得到了什么

I can't do the above condition because I can't retrieve the cities of the manager's company.我无法执行上述条件,因为我无法检索经理公司的城市。 However, I manage to get the manager's company, the city he wants to assign himself, but my $company->getCity() method returns my company object:但是,我设法获得了经理的公司,即他想要分配给自己的城市,但我的 $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
}

Re,回覆,

I "found" the solution to my problem, however I don't know if this is the right method to follow.我“找到”了我的问题的解决方案,但是我不知道这是否是正确的方法。 If anyone has a better idea for cleaner code, it's with great pleasure:如果有人对更简洁的代码有更好的想法,我很高兴:

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