使用FOSUserBundle和具有易于扩展的自定义实体创建关系时出错

时间:2018-08-17 13:38:37

标签: symfony orm fosuserbundle relation sonata

我一步步跟随this documentation奏鸣曲,并且奏效了。 然后我添加了一个新实体,并尝试生成与用户实体的多对多关系,当我验证它返回此错误时

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

这是我的两个实体

命名空间AppBundle \ Entity;

将Doctrine \ ORM \ Mapping用作ORM; 使用Gedmo \ Mapping \ Annotation作为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;

还有这个

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

}

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

这是我针对同一件事的代码,它可以正常工作。

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

    /**
     * @ORM\ManyToMany(targetEntity="wizai\WMC\UserBundle\Entity\User", mappedBy="customRoles", fetch="EAGER")
     */
    private $users;

    public function __construct()
    {
        $this->users = new ArrayCollection();
    }
}


class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * @ORM\ManyToMany(targetEntity="wizai\WMC\UserBundle\Entity\Role", inversedBy="users", fetch="EAGER")
     * @ORM\JoinTable(name="user_role",
     *      joinColumns={@ORM\JoinColumn(name="user", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="role", referencedColumnName="id")}
     * )
     */
    protected $customRoles;

    /**
     * User constructor.
     */
    public function __construct()
    {
        $this->customRoles = new ArrayCollection();
    }
}

如果仍然无法执行,可以先运行迁移或强制更新吗?

命令

  1. bin /控制台学说:migrations:diff
  2. bin /控制台学说:migrations:migrate

如果没有,请尝试。

  1. bin /控制台学说:schema:update --force --complete