Ducky
Ducky

Reputation: 409

Symfony one-to-many [Mapping] FAIL on doctrine:schema:validate

I have a one to many relation between Game and Expansion. A Game can have many expansions and a Expansion can belong to 1 game.

When I do bin/console doctrine:schema:validate, I get the following error:

[Mapping] FAIL - The entity-class 'AppBundle\Entity\Expansion' mapping is invalid: * The association AppBundle\Entity\Expansion#game refers to the inverse side field AppBundle\Entity\Game#expansions which doesn't exist.

I've been looking over and over again but I can't seem to find what's missing, I must be overlooking something...

Also, I've removed the inversedBy="expansions"- the error does not appear but it also isn't doing anything if I do a twig dump:

{{dump(game.expansions)}}

There's a relation in phpmyadmin:

enter image description here

enter image description here Game entity

<?php

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Game
 *
 * @ORM\Table(name="game")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\GameRepository")
 */
class Game
{
    /**
     * @ORM\OneToMany(targetEntity="PlayLog", mappedBy="game")
     * @ORM\OneToMany(targetEntity="Expansion", mappedBy="game")
     * @ORM\OrderBy({"date" = "DESC"})
     */
    private $playlogs;
    private $expansions;
    private $users;

    /**
     * @return ArrayCollection
     */
    public function getUsers()
    {
        return $this->users;
    }

    /**
     * @param ArrayCollection $users
     */
    public function setUsers($users)
    {
        $this->users = $users;
    }


    /**
     * Game constructor.
     */
    public function __construct()
    {
        $this->playlogs = new ArrayCollection();
        $this->expansions = new ArrayCollection();
        $this->users = new ArrayCollection();
    }

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @Assert\NotBlank()
     * @Assert\Length(
     *     min = "3",
     *  max = "100"
     * )
     * @ORM\Column(name="name", type="string", length=255, unique=true)
     */
    private $name;

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set name
     *
     * @param string $name
     *
     * @return Game
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }


    /**
     * @return mixed
     */
    public function getPlaylogs()
    {
        return $this->playlogs;
    }

    /**
     * @return mixed
     */
    public function getExpansions()
    {
        return $this->expansions;
    }


    public function addGameUser(User $user)
    {
        $this->users[] = $user;
    }
}

Expansion entity

<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Expansion
 *
 * @ORM\Table(name="expansion")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ExpansionRepository")
 */
class Expansion
{
    /**************************
     *  Relations
     *************************/

    /**
     * @ORM\ManyToOne(targetEntity="Game")
     * @ORM\JoinColumn(name="game_id", referencedColumnName="id")
     */

    private $game;

    /**************************
     *  Properties
     *************************/

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @Assert\NotBlank()
     * @Assert\Length(
     *     min = "3",
     *  max = "100"
     * )
     * @ORM\Column(name="name", type="string", length=255, unique=true)
     */
    private $name;

    /**************************
     *  Getters and setters
     *************************/
    /**
     * @return mixed
     */
    public function getGame()
    {
        return $this->game;
    }

    /**
     * @param mixed $game
     */
    public function setGame($game)
    {
        $this->game = $game;
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Expansion
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}

Upvotes: 0

Views: 296

Answers (1)

Shawn Northrop
Shawn Northrop

Reputation: 6036

Your Mapping on the Game Entity seems odd:

 /**
 * @ORM\OneToMany(targetEntity="PlayLog", mappedBy="game")
 * @ORM\OneToMany(targetEntity="Expansion", mappedBy="game")
 * @ORM\OrderBy({"date" = "DESC"})
 */
private $playlogs;
private $expansions;
private $users;

Try separating the mappings 1 per var:

 /**
 * @ORM\OneToMany(targetEntity="PlayLog", mappedBy="game")
 */
private $playlogs;

/**
* @ORM\OneToMany(targetEntity="Expansion", mappedBy="game")
*/
private $expansions;

/**
* @ORM\OrderBy({"date" = "DESC"})
*/
private $users;

Also I believe you need the inversedBy mapping on your Expansion Entity. See http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional for a similar example

Upvotes: 1

Related Questions