Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Doctrine2] Klucz obcy w encji., Single id is not allowed on composite primary key in entity
ghost1511
post 5.11.2014, 14:14:54
Post #1





Grupa: Zarejestrowani
Postów: 186
Pomógł: 18
Dołączył: 2.09.2010

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


Mam dwie encje:
  1. <?php
  2.  
  3. namespace Album\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Album\Entity\Song;
  8. use Doctrine\Common\Collections\Collection;
  9.  
  10. /**
  11.  * Description of Album
  12.  *
  13.  * @ORM\Entity
  14.  * @ORM\Table(name="Album")
  15.  */
  16. class Album {
  17.  
  18. /**
  19.   * @ORM\Id
  20.   * @ORM\Column(type="integer");
  21.   * @ORM\GeneratedValue(strategy="AUTO")
  22.   * @var integer
  23.   */
  24. protected $albumId;
  25.  
  26. /**
  27.   * @ORM\Column(type="string")
  28.   */
  29. protected $artist;
  30.  
  31. /**
  32.   * @ORM\Column(type="string")
  33.   */
  34. protected $title;
  35.  
  36. /**
  37.   *
  38.   * @var ArrayCollection
  39.   *
  40.   * @ORM\OneToMany(targetEntity="Song", mappedBy="album", cascade={"persist"})
  41.   */
  42. protected $songs;
  43.  
  44. function __construct() {
  45. $this->songs = new ArrayCollection();
  46. }
  47.  
  48. function getAlbumId() {
  49. return $this->albumId;
  50. }
  51.  
  52. function getArtist() {
  53. return $this->artist;
  54. }
  55.  
  56. function getTitle() {
  57. return $this->title;
  58. }
  59.  
  60. function setAlbumId($albumId) {
  61. $this->albumId = $albumId;
  62. }
  63.  
  64. function setArtist($artist) {
  65. $this->artist = $artist;
  66. }
  67.  
  68. function setTitle($title) {
  69. $this->title = $title;
  70. }
  71.  
  72. public function addSongs(Collection $songs) {
  73. foreach ($songs as $song) {
  74. $song->setAlbum($this);
  75. $this->songs->add($song);
  76. }
  77. }
  78.  
  79. public function removeSongs(Collection $songs) {
  80. foreach ($songs as $song) {
  81. $song->setAlbum(NULL);
  82. $this->songs->remove($song);
  83. }
  84. }
  85.  
  86. function getSongs() {
  87. return $this->songs;
  88. }
  89.  
  90. }

oraz:
  1. <?php
  2.  
  3. namespace Album\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Album\Entity\Album;
  7.  
  8. /**
  9.  * Description of Song
  10.  *
  11.  *
  12.  * @ORM\Entity
  13.  * @ORM\Table(name="Song")
  14.  */
  15. class Song {
  16.  
  17. /**
  18.   * @var string
  19.   * @ORM\Id
  20.   * @ORM\GeneratedValue(strategy="AUTO")
  21.   * @ORM\Column(type="string");
  22.   */
  23. protected $title;
  24.  
  25. /**
  26.   *
  27.   * @var Album
  28.   *
  29.   * @ORM\Id
  30.   * @ORM\GeneratedValue(strategy="NONE")
  31.   * @ORM\ManyToOne(targetEntity="Album", inversedBy="songs")
  32.   * @ORM\JoinColumn(name="albumId", referencedColumnName="albumId")
  33.   */
  34. protected $album;
  35.  
  36. function getTitle() {
  37. return $this->title;
  38. }
  39.  
  40. function getAlbum() {
  41. return $this->album;
  42. }
  43.  
  44. function setTitle($title) {
  45. $this->title = $title;
  46. }
  47.  
  48. function setAlbum(Album $album) {
  49. $this->album = $album;
  50. }
  51.  
  52.  
  53.  
  54. }


Oraz kontroler:
  1. <?php
  2.  
  3. namespace Album\Controller;
  4.  
  5. use Zend\Mvc\Controller\AbstractActionController;
  6. use Zend\View\Model\ViewModel;
  7. use Doctrine\ORM\EntityManager;
  8. use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
  9. use Album\Entity\Album;
  10.  
  11. class AlbumController extends AbstractActionController {
  12.  
  13. protected $doctrineHydrator;
  14.  
  15. public function indexAction() {
  16.  
  17. $killEmAllData = array(
  18. 'artist' => 'Metallica',
  19. 'title' => 'Kill em All',
  20. 'songs' => array(
  21. array('title' => 'Hit the Lights'),
  22. array('title' => 'The Four Horsemen'),
  23. array('title' => 'Motorbreath'),
  24. array('title' => 'Jump in the Fire'),
  25. array('title' => '(Anesthesia) Pulling Teeth'),
  26. array('title' => 'Whiplash'),
  27. array('title' => 'Phantom Lord'),
  28. array('title' => 'No Remorse'),
  29. array('title' => 'Seek & Destroy'),
  30. array('title' => 'Metal Militia'),
  31. array('title' => 'Am I Evil?'),
  32. array('title' => 'Blitzkrieg'),
  33. )
  34. );
  35.  
  36. var_dump($killEmAllData);
  37.  
  38. $killEmAll = new Album();
  39. $hydrator = $this->getDoctrineHydrator();
  40. $killEmAllHydrate = $hydrator->hydrate($killEmAllData, $killEmAll);
  41.  
  42. $songsCollection = new \Doctrine\Common\Collections\ArrayCollection();
  43. foreach ($killEmAllData['songs'] as $songArray) {
  44. $song = $hydrator->hydrate($songArray, new \Album\Entity\Song());
  45. $songsCollection->add($song);
  46. }
  47. $killEmAll->addSongs($songsCollection);
  48.  
  49. var_dump($killEmAllHydrate);
  50.  
  51. return new ViewModel();
  52. }
  53.  
  54. /**
  55.   * @var DoctrineORMEntityManager
  56.   */
  57. protected $entityManager;
  58.  
  59. public function getEntityManager() {
  60. if (null === $this->entityManager) {
  61. $this->entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
  62. }
  63. return $this->entityManager;
  64. }
  65.  
  66. /**
  67.   * @return DoctrineHydrator
  68.   */
  69. private function getDoctrineHydrator() {
  70. if (!$this->doctrineHydrator instanceof DoctrineHydrator) {
  71. $this->doctrineHydrator = new DoctrineHydrator($this->getEntityManager());
  72. }
  73. return $this->doctrineHydrator;
  74. }
  75.  
  76. }


I otrzymuję błąd:
Kod
Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Single id is not allowed on composite primary key in entity Album\Entity\Song' in D:\xampp1.8.2\htdocs\testy\DoctrineCompositeForeignKey\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php on line 414

Czy to ja robię coś źle? Czy to jakieś dziwne ograniczenie doctrine?

Ten post edytował ghost1511 5.11.2014, 14:15:44
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 Wersja Lo-Fi Aktualny czas: 27.06.2025 - 12:14