问题
I wanted to create a database relationship between fosuserbundle and my Leed entity. I created the entire relationship by make: entity and easily created relationships in the database. I can download both the user entity, the leed and the user entity leed.
The problem is currently in two cases. Both when I want to download all leeds simply or if I want to download leeds by the user.
SIMPLE GET ALL LEDS:
$leed = $this->getDoctrine()
->getRepository(Leed::class)
->find(1);
GET ERROR:
No mapping found for field 'leed' on class 'App\Application\Sonata\UserBundle\Entity\User'.
TRY TO GET THROUGH USER:
$leed = $user->getLeed();
GOT ERROR:
Return value of App\Application\Sonata\UserBundle\Entity\User::getLeed() must be an instance of App\Entity\Leed, null returned
MY LEED ENTITY:
<?php
namespace App\Entity;
use App\Application\Sonata\UserBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\LeedRepository")
*/
class Leed
{
...
/**
* @ORM\OneToOne(targetEntity="App\Application\Sonata\UserBundle\Entity\User", mappedBy="leed", cascade={"persist", "remove"})
*/
private $user;
public function getUser(): User
{
return $this->user;
}
public function setUser(User $user): self
{
$this->user = $user;
return $this;
}
MY USER ENTITY:
<?php
namespace App\Application\Sonata\UserBundle\Entity;
//use FOS\UserBundle\Model\User as BaseUser;
//use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use App\Entity\Leed;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* 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
*/
/**
* @ORM\Entity
* @ORM\Table(name="fos_user_user")
* @UniqueEntity(fields="company", message="Firma o tej nazwie już została zarejestrowana w systemie.")
*/
class User extends BaseUser
{
public function __construct()
{
parent::__construct();
$this->setRoles([]);
// $this->username = 'empty';
// $this->company = 'empty';
// $this->city = 'empty';
// $this->nip = 'empty';
// $this->postcode = 'empty';
// your code here
}
...
/**
* @ORM\OneToOne(targetEntity="App\Entity\Leed", inversedBy="user", cascade={"persist", "remove"})
*/
private $leed;
public function getLeed(): Leed
{
return $this->leed;
}
public function setLeed(Leed $leed): self
{
$this->leed = $leed;
return $this;
}
HERE THE MAPPING INFO OUTPUT:
Found 20 mapped entities:
[OK] App\Application\Sonata\UserBundle\Entity\User
[OK] App\Application\Sonata\UserBundle\Entity\Group
[OK] Sonata\UserBundle\Entity\BaseUser
[OK] Sonata\UserBundle\Entity\BaseGroup
[OK] Sonata\MediaBundle\Entity\BaseGallery
[OK] Sonata\MediaBundle\Entity\BaseGalleryHasMedia
[OK] Sonata\MediaBundle\Entity\BaseMedia
[OK] App\Application\Sonata\MediaBundle\Entity\Media
[OK] App\Application\Sonata\MediaBundle\Entity\Gallery
[OK] App\Application\Sonata\MediaBundle\Entity\GalleryHasMedia
[OK] App\Entity\Menu
[OK] App\Entity\TileLink
[OK] App\Entity\Order
[OK] App\Entity\Subpage
[OK] App\Entity\MenuItem
[OK] App\Entity\Leed
[OK] App\Entity\NewsletterEmail
[OK] App\Entity\Test
[OK] FOS\UserBundle\Model\User
[OK] FOS\UserBundle\Model\Group
Can anyone help me solve this or understand errors?
Ok i made User entity like that:
/**
* @ORM\OneToOne(targetEntity="App\Entity\Leed", inversedBy="user", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="leed_id", referencedColumnName="id", nullable=true)
* @ORM\Id
*/
private $leed;
public function getLeed(): ?Leed
{
return $this->leed;
}
public function setLeed(?Leed $leed): self
{
$this->leed = $leed;
return $this;
}
There is no error if i try:
$leed=$user->getLeed();
but that gives me a NULL
another situation is if i try to do:
$leed = $this->getDoctrine()
->getRepository(Leed::class)
->find(1);
i got error:
Notice: Undefined index: leed
i made enitis like:
user:
/**
* @ORM\OneToOne(targetEntity="Leed", mappedBy="user")
*/
private $leed;
public function getLeed(): ?Leed
{
return $this->leed;
}
public function setLeed(?Leed $leed): self
{
$this->leed = $leed;
return $this;
}
and leed like:
/**
* @ORM\OneToOne(targetEntity="App\Application\Sonata\UserBundle\Entity\User", inversedBy="leed")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
*/
private $user;
user entity:
/**
* @ORM\OneToOne(targetEntity="Leed", mappedBy="user")
* @ORM\JoinColumn(name="leed_id", nullable=true)
*/
private $leed;
/**
* @return mixed
*/
public function getLeed()
{
return $this->leed;
}
/**
* @param mixed $leed
*/
public function setLeed($leed): void
{
$this->leed = $leed;
}
and leed entity:
/**
* @ORM\OneToOne(targetEntity="App\Application\Sonata\UserBundle\Entity\User", inversedBy="leed")
* @ORM\JoinColumn(name="user_id", nullable=true)
*/
private $user;
回答1:
So now i have configuration like that:
Entity User:
/**
* @ORM\OneToOne(targetEntity="Leed", mappedBy="user")
* @ORM\JoinColumn(name="leed_id", nullable=true)
*/
private $leed;
Entity Leed:
/**
* @ORM\OneToOne(targetEntity="App\Application\Sonata\UserBundle\Entity\User", inversedBy="leed")
* @ORM\JoinColumn(name="user_id", nullable=true)
*/
private $user;
If i try to do:
$leed = $user->getLead();
I am getting NULL result, If i do:
$leed = $this->getDoctrine()
->getRepository(Leed::class)
->find(1);
i got error:
Notice: Undefined index: leed
This is seriously insane...
来源:https://stackoverflow.com/questions/62672274/one-to-one-between-fos-user-bundle-and-custom-entity-no-mapping-found