在 Symfony 3 中创建 FOSUserBundle 和自定义实体之间的多对多关系

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

我是第一次使用 FOSUserBundle,在尝试创建 ManyToMany 连接时遇到此错误

$ 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.                                                            

这是我的自定义实体业务

<?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';
    }
}

这是我的用户实体

<?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;
    }
}

我还有其他关于 FOSUserBundle 的问题,但这是我目前的主要问题。

提前致谢

经过一段时间的挖掘,这个问题找到了解决方案 里面config.yml添加

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

并且需要更改用户和组实体来管理注释

用户实体:

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;
    }
}

集团实体

<?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;
    }
}

最后像这样改变指定实体内部的关系

<?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';
    }
}

有效。很高兴解决这个问题。

$ bin/console doctrine:schema:validate

Mapping
-------


 [OK] The mapping files are correct.                                                                                    


Database
--------


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

有人帮助别人