Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [doctrine] problem z powiązaniami (inverse i owning side)
zwierzołak
post 21.10.2011, 19:44:28
Post #1





Grupa: Zarejestrowani
Postów: 78
Pomógł: 1
Dołączył: 25.03.2008

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


Witam,

Bawię się w sandboxie Doctrine 2.1 i chcę powiązać ze sobą odpowiednio "Entitiesy": Question i Answer. Uznałem, że relacja powinna być (patrząc od strony Question) OneToMany, bidirectional, Question po stronie Owning.

Jednak, kiedy stworzę Question i Answer, dodam Answer do Question i zapiszę Question, to zapisuje się on w bazie bez Answer:
  1. $q = new Entities\Question;
  2. $q->setTitle('Co jest grane?');
  3. $q->setDetails('szczegóły pytania');
  4.  
  5. $a = new Entities\Answer;
  6. $a->setText('Odpowiedź jest taka a taka.');
  7.  
  8. $q->addAnswer($a);
  9.  
  10. $em->persist($q);
  11. $em->flush();

Ponadto, jak zrobię odwrotnie i dodam Question do Answera i spróbuję zapisać Answer:
  1. $q = new Entities\Question;
  2. $q->setTitle('Co jest grane?');
  3. $q->setDetails('szczegóły pytania');
  4.  
  5. $a = new Entities\Answer;
  6. $a->setText('Odpowiedź jest taka a taka.');
  7.  
  8. $a->setQuestion($q);
  9.  
  10. $em->persist($a);
  11. $em->flush();

to dostaję błąd: InvalidArgumentException: A new entity was found through the relationship 'Entities\Answer#question' that was not configured to cascade persist operations for entity: Entities\Question@0000000006b2387700000000290a0be5. Explicitly persist the new entity or configure cascading persist operations on the relationship. If you cannot find out which entity causes the problem implement 'Entities\Question#__toString()' to get a clue. in H:\serwer\projekty\doctrine2-orm2\lib\Doctrine\ORM\UnitOfWork.php on line 576

Generalnie wygląda to dla mnie jakby Answer był po stronie Owning i jakby cascading był dobrze skonfigurowany, to by się zapisało Answer ze swoim Question...

Co zmienić, żeby powiązanie między Question a Answer działało prawidłowo? Poniżej kod omawianych Entities:
  1. namespace Entities;
  2.  
  3. class Answer
  4. {
  5. private $id;
  6. private $text;
  7.  
  8. /**
  9.   * @ManyToOne(targentEntity="Question", mappedBy="answers")
  10.   */
  11. private $question;
  12.  
  13. public function setQuestion(\Entities\Question $question)
  14. {
  15. $this->question = $question;
  16. }
  17.  
  18. public function getQuestion()
  19. {
  20. return $this->question;
  21. }
  22. }

  1. namespace Entities;
  2. use Doctrine\Common\Collections\ArrayCollection;
  3.  
  4. class Question
  5. {
  6. private $id;
  7. private $title;
  8. private $details;
  9.  
  10. /**
  11.   * @OneToMany(targentEntity="Answer", inversedBy="question")
  12.   */
  13. private $answers;
  14.  
  15. public function __construct()
  16. {
  17. $this->answers = new ArrayCollection();
  18. }
  19.  
  20. public function addAnswer($answer)
  21. {
  22. $answer->setQuestion($this);
  23. $this->answers[] = $answer;
  24. }
  25.  
  26. public function getAnswers()
  27. {
  28. return $this->answers;
  29. }
  30. }
Go to the top of the page
+Quote Post
Crozin
post 21.10.2011, 20:28:54
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


W Question powinieneś dodać parametr cascade={persist} w definicji relacji.
Go to the top of the page
+Quote Post
zwierzołak
post 23.10.2011, 22:55:27
Post #3





Grupa: Zarejestrowani
Postów: 78
Pomógł: 1
Dołączył: 25.03.2008

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


Mój problem polegał na tym, że ustawiłem w booststrapie, żeby mapowanie czytał z Yamla. I myślałem, że on to i tak tłumaczy sobie z Yamla na doc block annotations w Entities, więc zacząłem zmieniać bezpośrednio tam i się dziwiłem, że zmiany nic nie dają. cascade={persist} też się przydało.
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: 24.04.2024 - 04:49