Sirius
Sirius

Reputation: 653

Doctrine and Symfony2 : Association refers to inverse side that doesn't exist error

I have a page address.html.twig , the user can add many addresses in the table UserAddress. when he added his address in the database , the address should be render in the same page that he added his address then he can choose which one he would like to use. Unfortunately the address is not render.

First i thought that i have a problem in my controller action or in my twig page. I even asked a question here about it => here

I verified all my tables in phpmyadmin and all of them are well link but if i'm doing this: php app/console doctrine:schema:validate i have this error :

[Mapping] FAIL - The entity-class 'FLY\BookingsBundle\Entity\Commandes' mapping is invalid: * The association FLY\BookingsBundle\Entity\Commandes#user refers to the inverse side field Application\Sonata\UserBundle\Entity\User#commandes which does not exist.

[Mapping] FAIL - The entity-class 'FLY\BookingsBundle\Entity\UserAddress' mapping is invalid: * The association FLY\BookingsBundle\Entity\UserAddress#user refers to the inverse side field Application\Sonata\UserBundle\Entity\User#address which does not exist.

Have a look at this picture:

enter image description here

This is my UserAddress.php

/**
 * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="address")
 * @ORM\JoinColumn(nullable=true)
 */
private $user;

Commandes.php

/**
 * @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="commandes")
 * @ORM\JoinColumn(nullable=true)
 */
private $user;

User.php

/**
 * @ORM\Entity(repositoryClass="FLY\UserBundle\Repository\UserRepository")
 * @ORM\Table(name="fos_user_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */


    protected $id;

    public function __construct()
    {
        parent::__construct();
        $this->commandes = new \Doctrine\Common\Collections\ArrayCollection();
        $this->address = new \Doctrine\Common\Collections\ArrayCollection();
    }


    /**
     * @ORM\OneToMany(targetEntity="FLY\BookingsBundle\Entity\Commandes", mappedBy="user", cascade={"remove"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $commandes;



    /**
     * @ORM\OneToMany(targetEntity="FLY\BookingsBundle\Entity\UserAddress", mappedBy="user", cascade={"remove"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $address;

Here you can see my var dump:

User {#124 ▼
  #id: 21
  -commandes: null
  -address: null
}

Upvotes: 1

Views: 1047

Answers (3)

xarlymg89
xarlymg89

Reputation: 2617

It's quite likely this doesn't happen to anyone else, but there's a chance.

In my case, this error appeared because there was a duplicate. My entity had 2 fields, which are ManyToOne relationships. And they both had the same inversed names, which gave this error.

So this is the relevant bit of code:

class TaskIngredient
{

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Ingredient", inversedBy="taskIngredients")
     * @ORM\JoinColumn(nullable=false)
     */
    private $ingredient;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Task", inversedBy="taskIngredients")
     * @ORM\JoinColumn(nullable=false)
     */
    private $task;
}

The solution was relatively easy. I tried changing the inversedBy name, manually. However this didn't fix it (even after applying php app/console doctrine:schema:update --force and removing the var/cache folder).

So I just:

  1. Removed one of the problematic entities (and it's setter/getter)
  2. Ran the php bin/console make:entity tool and readded the field with a different name
  3. Voilà! Issue fixed.

Upvotes: 0

MistaJase
MistaJase

Reputation: 849

I've had an issue which has popped up 2-3 times in the last few years, where the mappings were incorrect but the schema update was successful. After the mappings were fixed this wasn't reflected in the schema and symfony assumed it was already up-to-date.

I recommend you try removing the relevent relationships manually from your user, commande and address tables and then run:

php app/console doctrine:schema:update --force

- it may fix your issue.

Upvotes: 1

MistaJase
MistaJase

Reputation: 849

Heres an example from one of my apps - I've done this for your commandes entity. You'll be able to piece together your UserAddress Entity from this example yourself!

Here goes:

User.php

 /**
 * @ORM\OneToMany(targetEntity="FLY\BookingsBundle\Entity\Commandes", mappedBy="commandesUser")
 */
protected $commandes;

User.php - Getters and Setters

/**
 * Add commandes
 *
 * @param FLY\BookingsBundle\Entity\Commandes $commandes
 */
public function addCommandes(\FLY\BookingsBundle\Entity\Commandes $commandes)
{
    $this->commandes[] = $commandes;
}

/**
 * Get commandes
 *
 * @return Doctrine\Common\Collections\Collection
 */
public function getCommandes()
{
    return $this->commandes;
}

Commandes.php

/**
* @ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="commandes")
* @ORM\JoinColumn(name="user", referencedColumnName="id")
*/
private $commandesUser;

Commandes.php - Getters and Setters

/**
 * Set commandesUser
 *
 * @param Application\Sonata\UserBundle\Entity\User $commandesUser
 */
public function setCommandesUser(\Application\Sonata\UserBundle\Entity\User $commandesUser = null)
{
    $this->commandesUser = $commandesUser;
}

/**
 * Get $commandesUser
 *
 * @return Application\Sonata\UserBundle\Entity\User
 */
public function getCommandesUser()
{
    return $this->commandesUser;
}

Upvotes: 0

Related Questions