Adrian Gonzalez
Adrian Gonzalez

Reputation: 65

mappings with doctrine OneToMany/ManyToOne

Im trying to fix a problem that symfony gives me with the mappings in doctrine

I got these 2 files: Gallery and Image where an Image correspondo to a gallery and a gallery has many Images (Simply , no?)

I followed some guides to set my firsts mapping on doctrine, and this is my result: Gallery.php:

/**
 * @ORM\OneToMany(targetEntity="Image", mappedBy="gallery")
 */
private $images;

public function __construct()
{
    $this->images = new \Doctrine\Common\Collections\ArrayCollection();
}

public function addImages(\multimediaBundle\Entity\Image $images)
{
    $this->images[] = $images;
}

public function getImages()
{
    return $this->images;
}

And Images.php :

/**
 * @ORM\ManyToOne(targetEntity="Gallery", inversedBy="Image")
 * @ORM\JoinColumn(name="gallery_id", referencedColumnName="id",onDelete="CASCADE")
 * @return integer
 */
private $gallery;

public function setGallery(\multimediaBundle\Entity\Gallery $gallery)
{
    $this->gallery = $gallery;
}

public function getGallery()
{
    return $this->gallery;
}

Symfony gives me 2 errors:

Class multimediaBundle\Entity\Gallery Mapping errors The mappings multimediaBundle\Entity\Gallery#images and multimediaBundle\Entity\Image#gallery are inconsistent with each other.

And

Class multimediaBundle\Entity\Image Mapping errors The association multimediaBundle\Entity\Image#gallery refers to the inverse side field multimediaBundle\Entity\Gallery#Image which does not exist.

I really dunno how bad this structure is since the delete on cascade works well when deleting a gallery, do you people have any clue for this mappings errors?

Thanks in advance

Upvotes: 1

Views: 296

Answers (1)

scoolnico
scoolnico

Reputation: 3125

Gallery.php

/**
 * @ORM\OneToMany(targetEntity="Image", mappedBy="gallery")
 */
private $images;

public function __construct()
{
    $this->images = new \Doctrine\Common\Collections\ArrayCollection();
}

Images.php

/**
 * @ORM\ManyToOne(targetEntity="Gallery", inversedBy="images")
 * @ORM\JoinColumn(name="gallery_id", referencedColumnName="id")
 */
private $gallery;

You have a typo (?) here inversedBy="Image"

To be replaced by inversedBy="images"

Upvotes: 1

Related Questions