Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SF][SF4.4] FOSUserBundle 2.2.x-dev i problem z encjami User oraz Group
swiezak
post
Post #1





Grupa: Zarejestrowani
Postów: 159
Pomógł: 0
Dołączył: 21.08.2011

Ostrzeżenie: (0%)
-----


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.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 08:04