Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF][SF4.4] FOSUserBundle 2.2.x-dev i problem z encjami User oraz Group
Forum PHP.pl > Forum > PHP > Frameworki
swiezak
Witam.
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:
  1. /**
  2.  * User
  3.  *
  4.  * @ORM\Table(name="fos_user")
  5.  * @ORM\Entity(repositoryClass="App\UserBundle\Repository\UserRepository")
  6.  */
  7. class User extends BaseUser
  8. {
  9. /**
  10.   * @ORM\ManyToMany(targetEntity="App\UserBundle\Entity\Group")
  11.   * @ORM\JoinTable(name="fos_user_user_group",
  12.   * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
  13.   * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
  14.   * )
  15.   */
  16. protected $groups;
  17.  
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. $this->groups = new ArrayCollection();
  22. }
  23.  
  24. /**
  25.   * @return Collection|Group[]
  26.   */
  27. public function getGroups(): Collection
  28. {
  29. return $this->groups;
  30. }
  31.  
  32. public function addGroup(Group $group): self
  33. {
  34. if (!$this->groups->contains($group)) {
  35. $this->groups[] = $group;
  36. }
  37.  
  38. return $this;
  39. }
  40.  
  41. public function removeGroup(Group $group): self
  42. {
  43. if ($this->groups->contains($group)) {
  44. $this->groups->removeElement($group);
  45. }
  46.  
  47. return $this;
  48. }
  49. }


Natomiast w Group widniał zapis:
  1. /**
  2.  * @ORM\Entity
  3.  * @ORM\Table(name="fos_group")
  4.  * @ORM\Entity(repositoryClass="App\UserBundle\Repository\GroupRepository")
  5.  */
  6. class Group extends BaseGroup
  7. {
  8. /**
  9.   * @ORM\Id
  10.   * @ORM\Column(type="integer")
  11.   * @ORM\GeneratedValue(strategy="AUTO")
  12.   */
  13. protected $id;
  14.  
  15. public function getId(): ?int
  16. {
  17. return $this->id;
  18. }
  19. }


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
  1. use FOS\UserBundle\Model\User as BaseUser;
  2. use Symfony\Component\Security\Core\User\UserInterface as BaseUserInterface;
  3. use FOS\UserBundle\Model\UserInterface as UserInterface;
  4. use FOS\UserBundle\Model\GroupInterface as GroupInterface;
  5. use FOS\UserBundle\Model\GroupableInterface as GroupableInterface;
  6.  
  7. /**
  8.  * User
  9.  *
  10.  * @ORM\Table(name="fos_user")
  11.  * @ORM\Entity(repositoryClass="App\UserBundle\Repository\UserRepository")
  12.  */
  13. class User extends BaseUser implements UserInterface, GroupableInterface
  14. {
  15. /**
  16.   * @var GroupInterface[]|Collection
  17.   */
  18. protected $groups;
  19.  
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. }
  24. }


Group
  1. use FOS\UserBundle\Model\Group as BaseGroup;
  2. use FOS\UserBundle\Model\GroupInterface as GroupInterface;
  3.  
  4. /**
  5.  * @ORM\Entity
  6.  * @ORM\Table(name="fos_group")
  7.  * @ORM\Entity(repositoryClass="App\UserBundle\Repository\GroupRepository")
  8.  */
  9. class Group extends BaseGroup implements GroupInterface
  10. {
  11. /**
  12.   * @ORM\Id
  13.   * @ORM\Column(type="integer")
  14.   * @ORM\GeneratedValue(strategy="AUTO")
  15.   */
  16. protected $id;
  17.  
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. }
  22. }


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:
  1. /**
  2.   * {@inheritdoc}
  3.   */
  4. public function getGroups()
  5. {
  6. return $this->groups ?: $this->groups = new ArrayCollection();
  7. }
  8.  
  9. /**
  10.   * {@inheritdoc}
  11.   */
  12. public function getGroupNames()
  13. {
  14. $names = [];
  15. foreach ($this->getGroups() as $group) {
  16. $names[] = $group->getName();
  17. }
  18.  
  19. return $names;
  20. }
  21.  
  22. /**
  23.   * {@inheritdoc}
  24.   */
  25. public function hasGroup($name)
  26. {
  27. return in_array($name, $this->getGroupNames());
  28. }
  29.  
  30. /**
  31.   * {@inheritdoc}
  32.   */
  33. public function addGroup(GroupInterface $group)
  34. {
  35. if (!$this->getGroups()->contains($group)) {
  36. $this->getGroups()->add($group);
  37. }
  38.  
  39. return $this;
  40. }
  41.  
  42. /**
  43.   * {@inheritdoc}
  44.   */
  45. public function removeGroup(GroupInterface $group)
  46. {
  47. if ($this->getGroups()->contains($group)) {
  48. $this->getGroups()->removeElement($group);
  49. }
  50.  
  51. return $this;
  52. }


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.
Pyton_000
Wywal `: self` z `addGroup` i zadziała.

A tak ogólnie to pozbądź się FOS i problem sam się rozwiąże wink.gif
swiezak
Dzięki za zainteresowanie tematem.

Wiem, że FOS w wersji 2 to kobyła z błędami i nie radzi sobie z SF 4.4, tym bardziej nie ma wsparcia dla SF 5, jednak ma się pojawić wersja 3, być może developerzy naprawią wszelkie niedogodności.
Na ten moment nie posiadam odpowiednich zasobów czasowych, aby napisać na nowo kod odpowiedzialny za rejestrację, logowanie, edycję profilu, przypomnienie hasła i weryfikację utworzonego konta dla userów. Jak zwykle wszystko "na wczoraj". W przyszłości to ogarnę, a teraz po prostu skopiowałem odpowiednie fragmenty z plików umieszczonych w katalogu vendor\friendsofsymfony\user-bundle\Model

Encje wyglądają u mnie tak - przedstawiam ważniejsze fragmenty (po przebudowaniu setterów i getterów poleceniem php bin/console make:entity --regenerate musiałem dokonać ręcznych korekt) i w tej chwili nie ma błędów podczas walidacji, jak również zachowana jest synchronizacja z bazą danych, a wtyczka wydaje się działać poprawnie:

User
  1. namespace App\UserBundle\Entity;
  2.  
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use FOS\UserBundle\Model\User as BaseUser;
  9. use FOS\UserBundle\Model\UserInterface;
  10. use FOS\UserBundle\Model\GroupInterface;
  11. use FOS\UserBundle\Model\GroupableInterface;
  12.  
  13. /**
  14.  * User
  15.  *
  16.  * @ORM\Table(name="fos_user")
  17.  * @ORM\Entity(repositoryClass="App\UserBundle\Repository\UserRepository")
  18.  */
  19. class User extends BaseUser
  20. {
  21. /**
  22.   * @var GroupInterface[]|Collection
  23.   *
  24.   * @ORM\ManyToMany(targetEntity="App\UserBundle\Entity\Group")
  25.   * @ORM\JoinTable(name="fos_user_group",
  26.   * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
  27.   * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
  28.   * )
  29.   */
  30. protected $groups;
  31.  
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. $this->groups = new ArrayCollection();
  36. }
  37.  
  38. /**
  39.   * {@inheritdoc}
  40.   */
  41. public function getGroups()
  42. {
  43. return $this->groups ?: $this->groups = new ArrayCollection();
  44. }
  45.  
  46. /**
  47.   * {@inheritdoc}
  48.   */
  49. public function getGroupNames()
  50. {
  51. $names = [];
  52. foreach ($this->getGroups() as $group) {
  53. $names[] = $group->getName();
  54. }
  55.  
  56. return $names;
  57. }
  58.  
  59. /**
  60.   * {@inheritdoc}
  61.   */
  62. public function hasGroup($name)
  63. {
  64. return in_array($name, $this->getGroupNames());
  65. }
  66.  
  67. /**
  68.   * {@inheritdoc}
  69.   */
  70. public function addGroup(GroupInterface $group)
  71. {
  72. if (!$this->getGroups()->contains($group)) {
  73. $this->getGroups()->add($group);
  74. }
  75.  
  76. return $this;
  77. }
  78.  
  79. /**
  80.   * {@inheritdoc}
  81.   */
  82. public function removeGroup(GroupInterface $group)
  83. {
  84. if ($this->getGroups()->contains($group)) {
  85. $this->getGroups()->removeElement($group);
  86. }
  87.  
  88. return $this;
  89. }
  90. }


Group
  1. namespace App\UserBundle\Entity;
  2.  
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Gedmo\Mapping\Annotation as Gedmo;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use FOS\UserBundle\Model\Group as BaseGroup;
  7. use FOS\UserBundle\Model\GroupInterface;
  8.  
  9. /**
  10.  * @ORM\Entity
  11.  * @ORM\Table(name="fos_group")
  12.  * @ORM\Entity(repositoryClass="App\UserBundle\Repository\GroupRepository")
  13.  */
  14. class Group extends BaseGroup
  15. {
  16. /**
  17.   * @ORM\Id
  18.   * @ORM\Column(type="integer")
  19.   * @ORM\GeneratedValue(strategy="AUTO")
  20.   */
  21. protected $id;
  22.  
  23. public function getId(): ?int
  24. {
  25. return $this->id;
  26. }
  27. }



Pozdrawiam.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2024 Invision Power Services, Inc.