Mam dwie encje:
<?php
namespace Album\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Album\Entity\Song;
use Doctrine\Common\Collections\Collection;
/**
* Description of Album
*
* @ORM\Entity
* @ORM\Table(name="Album")
*/
class Album {
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
* @var integer
*/
protected $albumId;
/**
* @ORM\Column(type="string")
*/
protected $artist;
/**
* @ORM\Column(type="string")
*/
protected $title;
/**
*
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Song", mappedBy="album", cascade={"persist"})
*/
protected $songs;
function __construct() {
$this->songs = new ArrayCollection();
}
function getAlbumId() {
return $this->albumId;
}
function getArtist() {
return $this->artist;
}
function getTitle() {
return $this->title;
}
function setAlbumId($albumId) {
$this->albumId = $albumId;
}
function setArtist($artist) {
$this->artist = $artist;
}
function setTitle($title) {
$this->title = $title;
}
public function addSongs(Collection $songs) {
foreach ($songs as $song) {
$song->setAlbum($this);
$this->songs->add($song);
}
}
public function removeSongs(Collection $songs) {
foreach ($songs as $song) {
$song->setAlbum(NULL);
$this->songs->remove($song);
}
}
function getSongs() {
return $this->songs;
}
}
oraz:
<?php
namespace Album\Entity;
use Doctrine\ORM\Mapping as ORM;
use Album\Entity\Album;
/**
* Description of Song
*
*
* @ORM\Entity
* @ORM\Table(name="Song")
*/
class Song {
/**
* @var string
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="string");
*/
protected $title;
/**
*
* @var Album
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
* @ORM\ManyToOne(targetEntity="Album", inversedBy="songs")
* @ORM\JoinColumn(name="albumId", referencedColumnName="albumId")
*/
protected $album;
function getTitle() {
return $this->title;
}
function getAlbum() {
return $this->album;
}
function setTitle($title) {
$this->title = $title;
}
function setAlbum(Album $album) {
$this->album = $album;
}
}
Oraz kontroler:
<?php
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Doctrine\ORM\EntityManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Album\Entity\Album;
class AlbumController extends AbstractActionController {
protected $doctrineHydrator;
public function indexAction() {
'artist' => 'Metallica',
'title' => 'Kill em All',
array('title' => 'Hit the Lights'), array('title' => 'The Four Horsemen'), array('title' => 'Motorbreath'), array('title' => 'Jump in the Fire'), array('title' => '(Anesthesia) Pulling Teeth'), array('title' => 'Whiplash'), array('title' => 'Phantom Lord'), array('title' => 'No Remorse'), array('title' => 'Seek & Destroy'), array('title' => 'Metal Militia'), array('title' => 'Am I Evil?'), array('title' => 'Blitzkrieg'), )
);
$killEmAll = new Album();
$hydrator = $this->getDoctrineHydrator();
$killEmAllHydrate = $hydrator->hydrate($killEmAllData, $killEmAll);
$songsCollection = new \Doctrine\Common\Collections\ArrayCollection();
foreach ($killEmAllData['songs'] as $songArray) {
$song = $hydrator->hydrate($songArray, new \Album\Entity\Song());
$songsCollection->add($song);
}
$killEmAll->addSongs($songsCollection);
return new ViewModel();
}
/**
* @var DoctrineORMEntityManager
*/
protected $entityManager;
public function getEntityManager() {
if (null === $this->entityManager) {
$this->entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
}
return $this->entityManager;
}
/**
* @return DoctrineHydrator
*/
private function getDoctrineHydrator() {
if (!$this->doctrineHydrator instanceof DoctrineHydrator) {
$this->doctrineHydrator = new DoctrineHydrator($this->getEntityManager());
}
return $this->doctrineHydrator;
}
}
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