Matt
Matt

Reputation: 1307

Symfony2/Doctrine won't create many-to-many relationship

I am trying to set up a many-to-many relationship in Symfony2 with Doctrine and MySQL. As far as I can tell my annotation follows the documentation exactly, and I have used this exact same syntax successfully before.

Does anybody see what is wrong with this?

MyVendor/MyBundle/Entity/Feed.php:

namespace MyVendor\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use MyVendor\MyBundle\Entity\Link;
/**
 * Feed
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Feed
{
    // ...
    /**
     * @ORM\ManyToMany(targetEntity="Link", mappedBy="feeds")
     * @ORM\JoinTable(name="feeds_links")
     */
    protected $links;
    // ...

MyVendor/MyBundle/Entity/Link.php:

namespace MyVendor\MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use MyVendor\MyBundle\Entity\Feed;
/**
 * Link
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Link
{
    // ...
    /**
     * @ORM\ManyToMany(targetEntity="Feed", mappedBy="links")
     */
    protected $feeds;
    // ...

But Doctrine does not create the relationship, doctrine:schema:update only generates this:

CREATE TABLE link (
    id INT AUTO_INCREMENT NOT NULL,
    -- ...
    PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE feed (
    id INT AUTO_INCREMENT NOT NULL,
    -- ...
    PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;

Upvotes: 1

Views: 1372

Answers (1)

Guillaume Fache
Guillaume Fache

Reputation: 813

There is a mistake in one of your annotation. One of them must be 'inversedBy', like this :

@ORM\ManyToMany(targetEntity="Feed", inversedBy="links")

Or :

@ORM\ManyToMany(targetEntity="Link", mappedBy="feeds")

I would recommend, from a general perspective, to use the SF toolbar , and especialy this :enter image description here

Click on this icon and it will tell you what problems your entities have, and point you directly to the annotations at fault.

Hope this helps.

Upvotes: 2

Related Questions