Search code examples
symfonyormentityfosuserbundle

Create a Many to Many relationship between FOSUserBundle and custom entity in Symfony 3


i'm using FOSUserBundle for first time and I'm trying to create a ManyToMany join and I get this error

$ bin/console doctrine:schema:validate

Mapping
-------

 [FAIL] The entity-class AppBundle\Entity\Business mapping is invalid:
 * The association AppBundle\Entity\Business#user refers to the owning side field Application\Sonata\UserBundle\Entity\User#business which does not exist.


Database
--------


 [OK] The database schema is in sync with the mapping files.                                                            

This is my custom entity Business

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * Business
 *
 * @ORM\Table(name="business")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\BusinessRepository")
 */
class Business
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="BusinessName", type="string", length=255)
     */
    private $businessName;

    /**
     * @var string
     *
     * @ORM\Column(name="fantasyName", type="string", length=255)
     */
    private $fantasyName;

    /**
     * @var string
     *
     * @ORM\Column(name="cuit", type="string", length=13)
     */
    private $cuit;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\BankAccountType", inversedBy="business")
     */
    private $bankAccountType;

    /**
     * @var \DateTime $created
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(type="datetime")
     */
    private $created;

    /**
     * @var \DateTime $updated
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(type="datetime")
     */
    private $updated;

    /**
     * @ORM\ManyToMany(targetEntity="\Application\Sonata\UserBundle\Entity\User", mappedBy="business")
     */
    private $user;

    /**
     * @var bool
     *
     * @ORM\Column(name="isActive", type="boolean")
     */
    private $isActive = true;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->user = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

    /**
     * Set businessName.
     *
     * @param string $businessName
     *
     * @return Business
     */
    public function setBusinessName($businessName)
    {
        $this->businessName = $businessName;

        return $this;
    }

    /**
     * Get businessName.
     *
     * @return string
     */
    public function getBusinessName()
    {
        return $this->businessName;
    }

    /**
     * Set fantasyName.
     *
     * @param string $fantasyName
     *
     * @return Business
     */
    public function setFantasyName($fantasyName)
    {
        $this->fantasyName = $fantasyName;

        return $this;
    }

    /**
     * Get fantasyName.
     *
     * @return string
     */
    public function getFantasyName()
    {
        return $this->fantasyName;
    }

    /**
     * Set cuit.
     *
     * @param string $cuit
     *
     * @return Business
     */
    public function setCuit($cuit)
    {
        $this->cuit = $cuit;

        return $this;
    }

    /**
     * Get cuit.
     *
     * @return string
     */
    public function getCuit()
    {
        return $this->cuit;
    }

    /**
     * Set created.
     *
     * @param \DateTime $created
     *
     * @return Business
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

    /**
     * Get created.
     *
     * @return \DateTime
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set updated.
     *
     * @param \DateTime $updated
     *
     * @return Business
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get updated.
     *
     * @return \DateTime
     */
    public function getUpdated()
    {
        return $this->updated;
    }

    /**
     * Set isActive.
     *
     * @param bool $isActive
     *
     * @return Business
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;

        return $this;
    }

    /**
     * Get isActive.
     *
     * @return bool
     */
    public function getIsActive()
    {
        return $this->isActive;
    }

    /**
     * Set bankAccountType.
     *
     * @param \AppBundle\Entity\BankAccountType|null $bankAccountType
     *
     * @return Business
     */
    public function setBankAccountType(\AppBundle\Entity\BankAccountType $bankAccountType = null)
    {
        $this->bankAccountType = $bankAccountType;

        return $this;
    }

    /**
     * Get bankAccountType.
     *
     * @return \AppBundle\Entity\BankAccountType|null
     */
    public function getBankAccountType()
    {
        return $this->bankAccountType;
    }

    /**
     * Add user.
     *
     * @param \Application\Sonata\UserBundle\Entity\User $user
     *
     * @return Business
     */
    public function addUser(\Application\Sonata\UserBundle\Entity\User $user)
    {
        $this->user[] = $user;

        return $this;
    }

    /**
     * Remove user.
     *
     * @param \Application\Sonata\UserBundle\Entity\User $user
     *
     * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
     */
    public function removeUser(\Application\Sonata\UserBundle\Entity\User $user)
    {
        return $this->user->removeElement($user);
    }

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

    public function __toString() {
        return isset($this->fantasyName)?$this->fantasyName:'Empresa Nueva';
    }
}

And this is my User entity

<?php

namespace Application\Sonata\UserBundle\Entity;


use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * This file has been generated by the SonataEasyExtendsBundle.
 *
 * @link https://sonata-project.org/easy-extends
 *
 * References:
 * @link http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
 */
class User extends BaseUser
{
    /**
     * @var int $id
     */
    protected $id;
    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Business", inversedBy="user")
     * @ORM\JoinTable(name="business_user")
     */
    private $business;

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
        $this->business = new \Doctrine\Common\Collections\ArrayCollection();
    }
    /**
     * Get id.
     *
     * @return int $id
     */
    public function getId()
    {
        return $this->id;
    }


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

    /**
     * @param mixed $business
     *
     * @return self
     */
    public function setBusiness($business)
    {
        $this->business = $business;

        return $this;
    }
}

I have other question about FOSUserBundle but this is my main problem at the moment.

Thanks in advance


Solution

  • After a while digging with this problem found a solution Inside config.yml added

    orm:
        entity_managers:
            default:
                mappings:
                    AppBundle: ~
                    SonataUserBundle: ~
                    FOSUserBundle: ~
                    ApplicationSonataUserBundle:
                        type: annotation
    

    And the needed to change the user and group entity to manage annotation

    User entity:

    namespace Application\Sonata\UserBundle\Entity;
    
    
    use Sonata\UserBundle\Entity\BaseUser as BaseUser;
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     * @ORM\Table(name="fos_user_user")
     *
     */
    class User extends BaseUser
    {
        /**
         * @var integer $id
         *
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
        /**
         * @var \Doctrine\Common\Collections\Collection
         *
         * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Business", mappedBy="users")
         */
        protected $business;
    
        /**
         * Constructor
         */
        public function __construct()
        {
            parent::__construct();
            $this->business = new \Doctrine\Common\Collections\ArrayCollection();
        }
        /**
         * Get id.
         *
         * @return int $id
         */
        public function getId()
        {
            return $this->id;
        }
    
    
        /**
         * @return mixed
         */
        public function getBusiness()
        {
            return $this->business;
        }
    
        /**
         * @param mixed $business
         *
         * @return self
         */
        public function setBusiness($business)
        {
            $this->business = $business;
    
            return $this;
        }
    }
    

    Group Entity

    <?php
    
    namespace Application\Sonata\UserBundle\Entity;
    
    use Sonata\UserBundle\Entity\BaseGroup as BaseGroup;
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     *
     * @ORM\Entity
     * @ORM\Table(name="fos_user_group")
     *
     */
    class Group extends BaseGroup
    {
        /**
         * @var integer $id
         *
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
        /**
         * Get id.
         *
         * @return int $id
         */
        public function getId()
        {
            return $this->id;
        }
    }
    

    and finally change the relationship inside the specify entity like this

    <?php
    
    namespace AppBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    use Gedmo\Mapping\Annotation as Gedmo;
    
    /**
     * Business
     *
     * @ORM\Table(name="business")
     * @ORM\Entity(repositoryClass="AppBundle\Repository\BusinessRepository")
     */
    class Business
    {
        /**
         * @var int
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @var string
         *
         * @ORM\Column(name="BusinessName", type="string", length=255)
         */
        private $businessName;
    
        /**
         * @var string
         *
         * @ORM\Column(name="fantasyName", type="string", length=255)
         */
        private $fantasyName;
    
        /**
         * @var string
         *
         * @ORM\Column(name="cuit", type="string", length=13)
         */
        private $cuit;
    
        /**
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\BankAccountType", inversedBy="business")
         */
        private $bankAccountType;
    
        /**
         * @var \DateTime $created
         *
         * @Gedmo\Timestampable(on="create")
         * @ORM\Column(type="datetime")
         */
        private $created;
    
        /**
         * @var \DateTime $updated
         *
         * @Gedmo\Timestampable(on="update")
         * @ORM\Column(type="datetime")
         */
        private $updated;
    
        /**
         * @var \Doctrine\Common\Collections\Collection
         *
         * @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="business")
         * @ORM\JoinTable(name="business_user",
         *   joinColumns={
         *     @ORM\JoinColumn(name="business_id", referencedColumnName="id")
         *   },
         *   inverseJoinColumns={
         *     @ORM\JoinColumn(name="user_id", referencedColumnName="id")
         *   }
         * )
         */    
        private $users;
    
        /**
         * @var bool
         *
         * @ORM\Column(name="isActive", type="boolean")
         */
        private $isActive = true;
    
        /**
         * Constructor
         */
        public function __construct()
        {
            $this->users = new \Doctrine\Common\Collections\ArrayCollection();
        }
    
        /**
         * Get id.
         *
         * @return int
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set businessName.
         *
         * @param string $businessName
         *
         * @return Business
         */
        public function setBusinessName($businessName)
        {
            $this->businessName = $businessName;
    
            return $this;
        }
    
        /**
         * Get businessName.
         *
         * @return string
         */
        public function getBusinessName()
        {
            return $this->businessName;
        }
    
        /**
         * Set fantasyName.
         *
         * @param string $fantasyName
         *
         * @return Business
         */
        public function setFantasyName($fantasyName)
        {
            $this->fantasyName = $fantasyName;
    
            return $this;
        }
    
        /**
         * Get fantasyName.
         *
         * @return string
         */
        public function getFantasyName()
        {
            return $this->fantasyName;
        }
    
        /**
         * Set cuit.
         *
         * @param string $cuit
         *
         * @return Business
         */
        public function setCuit($cuit)
        {
            $this->cuit = $cuit;
    
            return $this;
        }
    
        /**
         * Get cuit.
         *
         * @return string
         */
        public function getCuit()
        {
            return $this->cuit;
        }
    
        /**
         * Set created.
         *
         * @param \DateTime $created
         *
         * @return Business
         */
        public function setCreated($created)
        {
            $this->created = $created;
    
            return $this;
        }
    
        /**
         * Get created.
         *
         * @return \DateTime
         */
        public function getCreated()
        {
            return $this->created;
        }
    
        /**
         * Set updated.
         *
         * @param \DateTime $updated
         *
         * @return Business
         */
        public function setUpdated($updated)
        {
            $this->updated = $updated;
    
            return $this;
        }
    
        /**
         * Get updated.
         *
         * @return \DateTime
         */
        public function getUpdated()
        {
            return $this->updated;
        }
    
        /**
         * Set isActive.
         *
         * @param bool $isActive
         *
         * @return Business
         */
        public function setIsActive($isActive)
        {
            $this->isActive = $isActive;
    
            return $this;
        }
    
        /**
         * Get isActive.
         *
         * @return bool
         */
        public function getIsActive()
        {
            return $this->isActive;
        }
    
        /**
         * Set bankAccountType.
         *
         * @param \AppBundle\Entity\BankAccountType|null $bankAccountType
         *
         * @return Business
         */
        public function setBankAccountType(\AppBundle\Entity\BankAccountType $bankAccountType = null)
        {
            $this->bankAccountType = $bankAccountType;
    
            return $this;
        }
    
        /**
         * Get bankAccountType.
         *
         * @return \AppBundle\Entity\BankAccountType|null
         */
        public function getBankAccountType()
        {
            return $this->bankAccountType;
        }
    
        /**
         * Add user.
         *
         * @param \Application\Sonata\UserBundle\Entity\User $user
         *
         * @return Business
         */
        public function addUser(\Application\Sonata\UserBundle\Entity\User $user)
        {
            $this->users[] = $user;
    
            return $this;
        }
    
        /**
         * Remove user.
         *
         * @param \Application\Sonata\UserBundle\Entity\User $user
         *
         * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
         */
        public function removeUser(\Application\Sonata\UserBundle\Entity\User $user)
        {
            return $this->users->removeElement($user);
        }
    
        /**
         * Get user.
         *
         * @return \Doctrine\Common\Collections\Collection
         */
        public function getUsers()
        {
            return $this->users;
        }
    
        public function __toString() {
            return isset($this->fantasyName)?$this->fantasyName:'Empresa Nueva';
        }
    }
    

    It works. More than happy to solve this.

    $ bin/console doctrine:schema:validate
    
    Mapping
    -------
    
    
     [OK] The mapping files are correct.                                                                                    
    
    
    Database
    --------
    
    
     [OK] The database schema is in sync with the mapping files.                                                            
    

    Some help someone