Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [inny][SF2] Relacja N:M CRUD - problem
Forum PHP.pl > Forum > PHP > Frameworki
basso
Witam,
Wygenerowałem dzisiaj sobie CRUD z relacją N:M.
Hmm wszystko spoko wygnerowało, działa listowanie i to wszystko.
Jak daje nowy to mam wieczny problem:
W przypadku Tworzenia Usera
  1. [2/2] StringCastException: A "__toString()" method was not found on the objects of type "Multimedia\AccountBundle\Entity\Group" passed to the choice field. To read a custom getter instead, set the option "property" to the desired property path. -+


W przypadku tworzenia grupy
  1. A "__toString()" method was not found on the objects of type "Multimedia\AccountBundle\Entity\User" passed to the choice field. To read a custom getter instead, set the option "property" to the desired property path.


Z tutoriala, z książki i z filmów youtube robię => non stop to samo sad.gif. Ręce opadają.


Relacje w Encjach zapisuje jako:
Tabela Users
class User implements UserInterface, \Serializable
{
  1. /**
  2.   * @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
  3.   *
  4.   */
  5. private $groups;
  6.  
  7. itd.



Tabela group:
  1. /**
  2.  * @ORM\Table(name="groups")
  3.  * @ORM\Entity()
  4.  */
  5. class Group implements RoleInterface
  6. {
  7.  
  8. /**
  9.   * @ORM\ManyToMany(targetEntity="User", mappedBy="groups" )
  10.   */
  11. private $users;
  12.  
  13. itd.



Pomocy.
thek
A settery i gettery wygenerowane prawidłowo? Bo przecież jakoś jedną encję do drugiej musisz zapisać i pewnie może się tutaj pluć.
destroyerr
Cytat
A settery i gettery wygenerowane prawidłowo? Bo przecież jakoś jedną encję do drugiej musisz zapisać i pewnie może się tutaj pluć.


Nie ma potrzeby się tak rozpędzać. Komunikat błędu jasno stwierdza, że brakuje metody __toString (najprawdopodobniej jest potrzebna w formularzu do wyrenderowania listy wyboru), ewentualnie zmiana konfiguracji.
mortus
Dokładnie w klasie Group brakuje magicznej metody __toString(), trzeba tą magiczną metodę sobie dopisać, bo generator jej nie generuje.
basso
Wiam, setery i getery prawidłowo.
Jak wrócę do domu to spróbuję dodać tą metodę __String() i umieszczę całą encję.

dzięki za reakcję.
thek
Nie wiem co dokładnie i w jakim momencie się burzy, ale może wystarczy przy FormType dla tegoż formularza przy polu podeprzeć się property czy też property_path? Niedawno korzystałem po raz piewszy z tego, więc nie bić jeśli się mylę smile.gif
basso
Witam,
Dzięki za info, zadziałało smile.gif
Jeszcze jedno mam pytanko.
Mam te 2 klasy:User i Group

I Mam many to many.

1. W users bez problemu mogę wybierać różne grupy dla różnych userów.
2. Natomiast w group nie mogę sobie wybierać różnych userów do różnych grup. Jak zapisuje to nie zapisuje mi zmian hmmm.
Ale te multiselecty widzi i tu i tu.


User
  1. <?php
  2.  
  3. namespace Multimedia\AccountBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
  8. /**
  9.  * Multimedia\AccountBundle\Entity\User
  10.  *
  11.  * @ORM\Table(name="user")
  12.  * @ORM\Entity(repositoryClass="Multimedia\AccountBundle\Entity\UserRepository")
  13.  */
  14. class User implements UserInterface, \Serializable
  15. {
  16. /**
  17.   * @ORM\Column(type="integer")
  18.   * @ORM\Id
  19.   * @ORM\GeneratedValue(strategy="AUTO")
  20.   */
  21. private $id;
  22.  
  23. /**
  24.   * @ORM\Column(type="string", length=25, unique=true)
  25.   */
  26. private $username;
  27.  
  28. /**
  29.   * @ORM\Column(type="string", length=32)
  30.   */
  31. private $salt;
  32.  
  33. /**
  34.   * @ORM\Column(type="string", length=40)
  35.   */
  36. private $password;
  37.  
  38. /**
  39.   * @ORM\Column(type="string", length=60, unique=true)
  40.   */
  41. private $email;
  42.  
  43. /**
  44.   * @ORM\Column(name="is_active", type="boolean")
  45.   */
  46. private $isActive;
  47.  
  48.  
  49. /**
  50.   * @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
  51.   *
  52.   */
  53. private $groups;
  54.  
  55.  
  56. public function __construct()
  57. {
  58. $this->isActive = true;
  59. $this->salt = md5(uniqid(null, true));
  60. }
  61.  
  62. /**
  63.   * @inheritDoc
  64.   */
  65. public function getUsername()
  66. {
  67. return $this->username;
  68. }
  69.  
  70. /**
  71.   * @inheritDoc
  72.   */
  73. public function getSalt()
  74. {
  75. return $this->salt;
  76. }
  77.  
  78. /**
  79.   * @inheritDoc
  80.   */
  81. public function getPassword()
  82. {
  83. return $this->password;
  84. }
  85.  
  86. /**
  87.   * @inheritDoc
  88.   */
  89. public function getRoles()
  90. {
  91. return array('ROLE_USER');
  92. }
  93.  
  94. /**
  95.   * @inheritDoc
  96.   */
  97. public function eraseCredentials()
  98. {
  99. }
  100.  
  101. /**
  102.   * @inheritDoc
  103.   */
  104. public function equals(UserInterface $user)
  105. {
  106. return $this->username === $user->getUsername();
  107. }
  108.  
  109. /**
  110.   * @see \Serializable::serialize()
  111.   */
  112. public function serialize()
  113. {
  114. return serialize(array(
  115. $this->id,
  116. ));
  117. }
  118.  
  119. /**
  120.   * @see \Serializable::unserialize()
  121.   */
  122. public function unserialize($serialized)
  123. {
  124. list (
  125. $this->id,
  126. ) = unserialize($serialized);
  127. }
  128.  
  129. /**
  130.   * Get id
  131.   *
  132.   * @return integer
  133.   */
  134. public function getId()
  135. {
  136. return $this->id;
  137. }
  138.  
  139. /**
  140.   * Set username
  141.   *
  142.   * @param string $username
  143.   * @return User
  144.   */
  145. public function setUsername($username)
  146. {
  147. $this->username = $username;
  148.  
  149. return $this;
  150. }
  151.  
  152. /**
  153.   * Set salt
  154.   *
  155.   * @param string $salt
  156.   * @return User
  157.   */
  158. public function setSalt($salt)
  159. {
  160. $this->salt = $salt;
  161.  
  162. return $this;
  163. }
  164.  
  165. /**
  166.   * Set password
  167.   *
  168.   * @param string $password
  169.   * @return User
  170.   */
  171. public function setPassword($password)
  172. {
  173. $encoder = new MessageDigestPasswordEncoder('sha1',false,1);
  174. $password= $encoder->encodePassword($password, $this->salt);
  175. $this->password = $password;
  176.  
  177. return $this;
  178. }
  179.  
  180. /**
  181.   * Set email
  182.   *
  183.   * @param string $email
  184.   * @return User
  185.   */
  186. public function setEmail($email)
  187. {
  188. $this->email = $email;
  189.  
  190. return $this;
  191. }
  192.  
  193. /**
  194.   * Get email
  195.   *
  196.   * @return string
  197.   */
  198. public function getEmail()
  199. {
  200. return $this->email;
  201. }
  202.  
  203. /**
  204.   * Set isActive
  205.   *
  206.   * @param boolean $isActive
  207.   * @return User
  208.   */
  209. public function setIsActive($isActive)
  210. {
  211. $this->isActive = $isActive;
  212.  
  213. return $this;
  214. }
  215.  
  216. /**
  217.   * Get isActive
  218.   *
  219.   * @return boolean
  220.   */
  221. public function getIsActive()
  222. {
  223. return $this->isActive;
  224. }
  225.  
  226.  
  227.  
  228.  
  229. /**
  230.   * Add groups
  231.   *
  232.   * @param Multimedia\AccountBundle\Entity\Group $groups
  233.   * @return User
  234.   */
  235. public function addGroup(\Multimedia\AccountBundle\Entity\Group $groups)
  236. {
  237. $this->groups[] = $groups;
  238.  
  239. return $this;
  240. }
  241.  
  242. /**
  243.   * Remove groups
  244.   *
  245.   * @param Multimedia\AccountBundle\Entity\Group $groups
  246.   */
  247. public function removeGroup(\Multimedia\AccountBundle\Entity\Group $groups)
  248. {
  249. $this->groups->removeElement($groups);
  250. }
  251.  
  252. /**
  253.   * Get groups
  254.   *
  255.   * @return Doctrine\Common\Collections\Collection
  256.   */
  257. public function getGroups()
  258. {
  259. return $this->groups;
  260. }
  261.  
  262.  
  263. /**
  264.   *@return string
  265.   */
  266. public function __toString() {
  267.  
  268. return $this->getUsername();
  269. }
  270. }



I GROUP:

  1. <?php
  2. namespace Multimedia\AccountBundle\Entity;
  3. use Symfony\Component\Security\Core\Role\RoleInterface;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping as ORM;
  6.  
  7. /**
  8.  * @ORM\Table(name="groups")
  9.  * @ORM\Entity()
  10.  */
  11. class Group implements RoleInterface
  12. {
  13. /**
  14.   * @ORM\Column(name="id", type="integer")
  15.   * @ORM\Id()
  16.   * @ORM\GeneratedValue(strategy="AUTO")
  17.   */
  18. private $id;
  19.  
  20. /**
  21.   * @ORM\Column(name="name", type="string", length=30)
  22.   */
  23. private $name;
  24.  
  25. /**
  26.   * @ORM\Column(name="role", type="string", length=20, unique=true)
  27.   */
  28. private $role;
  29.  
  30. /**
  31.   * @ORM\ManyToMany(targetEntity="User", mappedBy="groups")
  32.   */
  33. private $users;
  34.  
  35. public function __construct()
  36. {
  37. $this->users = new ArrayCollection();
  38. }
  39.  
  40. // ... getters and setters for each property
  41.  
  42. /**
  43.   * @see RoleInterface
  44.   */
  45. public function getRole()
  46. {
  47. return $this->role;
  48. }
  49.  
  50. /**
  51.   * Get id
  52.   *
  53.   * @return integer
  54.   */
  55. public function getId()
  56. {
  57. return $this->id;
  58. }
  59.  
  60. /**
  61.   * Set name
  62.   *
  63.   * @param string $name
  64.   * @return Group
  65.   */
  66. public function setName($name)
  67. {
  68. $this->name = $name;
  69.  
  70. return $this;
  71. }
  72.  
  73. /**
  74.   * Get name
  75.   *
  76.   * @return string
  77.   */
  78. public function getName()
  79. {
  80. return $this->name;
  81. }
  82.  
  83. /**
  84.   * Set role
  85.   *
  86.   * @param string $role
  87.   * @return Group
  88.   */
  89. public function setRole($role)
  90. {
  91. $this->role = $role;
  92.  
  93. return $this;
  94. }
  95.  
  96. /**
  97.   * Add users
  98.   *
  99.   * @param Multimedia\AccountBundle\Entity\User $users
  100.   * @return Group
  101.   */
  102. public function addUser(\Multimedia\AccountBundle\Entity\User $users)
  103. {
  104. $this->users[] = $users;
  105.  
  106. return $this;
  107. }
  108.  
  109. /**
  110.   * Remove users
  111.   *
  112.   * @param Multimedia\AccountBundle\Entity\User $users
  113.   */
  114. public function removeUser(\Multimedia\AccountBundle\Entity\User $users)
  115. {
  116. $this->users->removeElement($users);
  117. }
  118.  
  119. /**
  120.   * Get users
  121.   *
  122.   * @return Doctrine\Common\Collections\Collection
  123.   */
  124. public function getUsers()
  125. {
  126. return $this->users;
  127. }
  128.  
  129.  
  130.  
  131. /**
  132.   *@return string
  133.   */
  134. public function __toString() {
  135.  
  136. return $this->getName();
  137. }
  138. }
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-2025 Invision Power Services, Inc.