Создайте отношение «многие ко многим» между FOSUserBundle и пользовательским объектом в Symfony 3 - PullRequest
0 голосов
/ 29 августа 2018

Я впервые использую 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, но сейчас это моя главная проблема.

Заранее спасибо

1 Ответ

0 голосов
/ 07 сентября 2018

Через некоторое время копаясь с этой проблемой нашел решение Внутри 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.                                                            

Кто-то помогает кому-то

...