Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [SF][SF2][Symfony2] buildForm problem, Object of class could not be converted to string
damianooo
post 19.06.2017, 20:47:06
Post #1





Grupa: Zarejestrowani
Postów: 493
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


Połączyłem encję MEET (mecz) z nową encją LEAGUE (liga) i w CRUD dla funkcji CREATE muszę teraz dodać w formularzu nowe pole w którym będę mógł wybrać rodzaj ligi dla meczu.

Po dodaniu w klasie MeetType nowego pola dostaję jednak komunikat z poniższym błędem.
Nie do końca rozumiem komunikat tzn. w którym miejscu klasa nie może być konwertowana do typu String ?
  1. Catchable Fatal Error: Object of class AppBundle\Entity\League could not be converted to string


Klasa formularza - MeetType.php :

  1. class MeetType extends AbstractType
  2. {
  3. /**
  4.   * {@inheritdoc}
  5.   */
  6. public function buildForm(FormBuilderInterface $builder, array $options)
  7. {
  8. $builder->add('name')
  9. ->add('matchday')
  10. ->add('league')


klasa entity MEET - Meet.php :
  1. /**
  2.   * @ORM\ManyToOne(
  3.   * targetEntity = "League",
  4.   * )
  5.   *
  6.   * @ORM\JoinColumn(
  7.   * name = "league_id",
  8.   * referencedColumnName = "id",
  9.   * onDelete = "SET NULL"
  10.   * )
  11.   */
  12. private $league;


próbowałem też tak:

  1. ->add('league', 'collection', array(
  2. 'type' => new League(),
  3. 'allow_add' => true,
  4. 'allow_delete' => true,
  5. 'by_reference' => false,)
  6. )


ale wtedy dostaję podobny komunikat:

  1. Expected argument of type "string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface", "AppBundle\Entity\League" given


Ten post edytował damianooo 19.06.2017, 20:47:41
Go to the top of the page
+Quote Post
ohm
post 19.06.2017, 20:50:34
Post #2





Grupa: Zarejestrowani
Postów: 618
Pomógł: 143
Dołączył: 22.12.2010

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


dodaj do Leauge.php __toString() smile.gif Do innych też możesz profilaktycznie wink.gif
Go to the top of the page
+Quote Post
Pyton_000
post 19.06.2017, 20:56:26
Post #3





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


A te rozwiazania nie pomagają? https://symfony.com/doc/2.8/form/form_collections.html
Go to the top of the page
+Quote Post
damianooo
post 19.06.2017, 21:11:54
Post #4





Grupa: Zarejestrowani
Postów: 493
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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



  1. public function __toString() {
  2. return $this->getName();
  3. }


pomogło ...

Czemu aby to zadziałało należało użyć metody magicznej __toString() ?
Rozumiem że metoda ta konwertuje z typu Obiekt na String ale czemu akurat Symfony miało z tym problem tutaj ? Pewnie to nie kwestia Symfony.
Go to the top of the page
+Quote Post
ohm
post 19.06.2017, 21:34:34
Post #5





Grupa: Zarejestrowani
Postów: 618
Pomógł: 143
Dołączył: 22.12.2010

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


Ogólnie rzecz ujmując, bez metody magicznej symfony nie wie co ma robić z tym obiektem, więc rzuca exceptiona.
Go to the top of the page
+Quote Post
Pyton_000
post 19.06.2017, 21:53:09
Post #6





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


To powinno działać bez __toString dlatego że tutaj na sztywno piszesz co chcesz. Problemem będzie jak będziesz się chciał do czegoś innego odwołać.

Radzę jednak zerknąć dokumentację którą podesłałem i robić małymi kroczkami.
Go to the top of the page
+Quote Post
Pilsener
post 20.06.2017, 07:54:55
Post #7





Grupa: Zarejestrowani
Postów: 1 590
Pomógł: 185
Dołączył: 19.04.2006
Skąd: Gdańsk

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


Problem powstał, bo został użyty typ prosty w formularzu zamiast np. EntityType::class

Następnie EntityType konfigurujemy tak, aby:
- wyświetlał odpowiedni label (np. z getFullName)
- renderował się jako select, radio czy też checkbox

Jest to dobrze opisane w dokumentacji:
http://symfony.com/doc/current/reference/f...pes/entity.html
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 18.04.2024 - 20:28