Dokonuję migracji z wersji 3.4 frameworka na wersję 4.4 i zaciąłem się na małej popierdółce.
Byłem zmuszony nieco przerobić klasy User i Group w customowym bundlu, którego używam do zarządzania userami, jednak podczas walidacji (php bin/console doctrine:schema:validate) wyskakuje błąd z interfejsami w tle:
Cytat
Compile Error: Declaration of App\UserBundle\Entity\User::addGroup(App\UserBundle\Entity\Group $group): App\UserBundle\Entity\User must be compatible with FOS\UserBundle\Model\User::addGroup(FOS\UserBundle\Model\GroupInterface $group)
Pierwotnie w encji User miałem zapis:
/** * User * * @ORM\Table(name="fos_user") * @ORM\Entity(repositoryClass="App\UserBundle\Repository\UserRepository") */ class User extends BaseUser { /** * @ORM\ManyToMany(targetEntity="App\UserBundle\Entity\Group") * @ORM\JoinTable(name="fos_user_user_group", * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")} * ) */ protected $groups; public function __construct() { parent::__construct(); $this->groups = new ArrayCollection(); } /** * @return Collection|Group[] */ public function getGroups(): Collection { return $this->groups; } public function addGroup(Group $group): self { if (!$this->groups->contains($group)) { $this->groups[] = $group; } return $this; } public function removeGroup(Group $group): self { if ($this->groups->contains($group)) { $this->groups->removeElement($group); } return $this; } }
Natomiast w Group widniał zapis:
/** * @ORM\Entity * @ORM\Table(name="fos_group") * @ORM\Entity(repositoryClass="App\UserBundle\Repository\GroupRepository") */ class Group extends BaseGroup { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; public function getId(): ?int { return $this->id; } }
Przejrzałem pliki z modelami od wtyczki FOSUserBundle i tam nastąpiła zmiana - pojawiły się interfejsy.
Próbowałem dokonać korekt w encjach w poniższy sposób i przy użyciu polecenia php bin/console make:entity --regenerate utworzyć gettery i settery:
User
use FOS\UserBundle\Model\User as BaseUser; use Symfony\Component\Security\Core\User\UserInterface as BaseUserInterface; use FOS\UserBundle\Model\UserInterface as UserInterface; use FOS\UserBundle\Model\GroupInterface as GroupInterface; use FOS\UserBundle\Model\GroupableInterface as GroupableInterface; /** * User * * @ORM\Table(name="fos_user") * @ORM\Entity(repositoryClass="App\UserBundle\Repository\UserRepository") */ class User extends BaseUser implements UserInterface, GroupableInterface { /** * @var GroupInterface[]|Collection */ protected $groups; public function __construct() { parent::__construct(); } }
Group
use FOS\UserBundle\Model\Group as BaseGroup; use FOS\UserBundle\Model\GroupInterface as GroupInterface; /** * @ORM\Entity * @ORM\Table(name="fos_group") * @ORM\Entity(repositoryClass="App\UserBundle\Repository\GroupRepository") */ class Group extends BaseGroup implements GroupInterface { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; public function __construct() { parent::__construct(); } }
Jednak w encji User nie pojawiają się oczekiwane rezultaty - nie ma kolekcji dla groups, ani setterów i getterów, ponadto brak jest odpowiedniej tabeli w bazie danych.
Niby dopisałem "z palca" do encji User fragment:
/** * {@inheritdoc} */ public function getGroups() { return $this->groups ?: $this->groups = new ArrayCollection(); } /** * {@inheritdoc} */ public function getGroupNames() { $names = []; foreach ($this->getGroups() as $group) { $names[] = $group->getName(); } return $names; } /** * {@inheritdoc} */ public function hasGroup($name) { } /** * {@inheritdoc} */ public function addGroup(GroupInterface $group) { if (!$this->getGroups()->contains($group)) { $this->getGroups()->add($group); } return $this; } /** * {@inheritdoc} */ public function removeGroup(GroupInterface $group) { if ($this->getGroups()->contains($group)) { $this->getGroups()->removeElement($group); } return $this; }
ale dlaczego tak się dzieje, że nie pojawia się to automatycznie po przebudowaniu modelu? Zapewne robię jakiś prosty błąd, którego nie widzę na ten moment.
Ktoś z Was walczył z FOSUserBundle na SF 4.4 i wie, jak pozbyć się wspomnianych niedogodności? Będę wdzięczny za poświęcony czas i pomoc.