【问题标题】:Error creating relationship with FOSUserBundle and custom entity with easy-extends与 FOSUserBundle 和易于扩展的自定义实体创建关系时出错
【发布时间】:2019-01-24 13:45:06
【问题描述】:

我在奏鸣曲中跟随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;
    }

}

有什么想法吗?

【问题讨论】:

  • 能否分享一下你的整个实体类
  • 这是没有getter和setter的整个实体
  • 但它在您的业务实体中包含构造函数

标签: symfony orm fosuserbundle relation sonata


【解决方案1】:

这是我的代码,它可以工作。

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/console 原则:迁移:diff
  2. bin/控制台原则:迁移:迁移

如果没有,请尝试。

  1. bin/console 原则:schema:update --force --complete

【讨论】:

  • “doctrine:migrations”命名空间中没有定义命令。更新 --force 有效,但验证无效
  • 您的数据库中是否有为 user_business 创建的中间表?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
相关资源
最近更新 更多