[英]Symfony $this->getUser() without doctrine associations (OneToMany)
In Symfony, we can get current logged-in user data using $this->getUser(), but my problem is when I access this statement, I am getting all the user-associated data set.在 Symfony 中,我们可以使用 $this->getUser() 获取当前登录的用户数据,但我的问题是当我访问此语句时,我获取了所有与用户相关的数据集。 which has OneToMany relationships with another entity, and it has a lot of data.它与另一个实体具有 OneToMany 关系,并且它有很多数据。
Example:例子:
User Entity用户实体
` `
class User implements UserInterface
{
/**
* @var string
* @ORM\Id
* @ORM\Column(type="string", length=10)
*
*/
protected $id;
/**
* @var string
* @ORM\Column(type="string")
*/
protected $email;
/**
* @var array
* @ORM\Column(type="json")
*/
protected $roles;
/**
* One User has Many Posts.
* @ORM\OneToMany(targetEntity="App\Entity\Post", mappedBy="user", fetch="LAZY")
*
*
*/
private Collection $posts;
` `
Post Entity发布实体
` `
class Post
{
/**
* @var string
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", length=11)
*/
private $id;
/**
* Many posts have one user.
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="post", fetch="EXTRA_LAZY")
* @ORM\JoinColumn(name="userId", referencedColumnName="id")
*/
private $user;
` `
I am looking to get rid of the user-associated data set or limit the associated data set to limit 1.我希望摆脱与用户关联的数据集或将关联数据集限制为 limit 1。
Thank you for the help in advance.提前感谢您的帮助。 :) :)
found solution after hours of search.经过数小时的搜索找到了解决方案。
You will be required to add Symfony Serializer #Ignore attribute on the Entity class.您将需要在实体 class 上添加 Symfony Serializer #Ignore 属性。
Example例子
use Symfony\Component\Serializer\Annotation\Ignore;
class User implements UserInterface
{
/**
* @var string
*
*/
#[ORM\Id]
#[ORM\Column(type: 'string', length: 10)]
protected $id;
/**
* @var string
*/
#[ORM\Column(type: 'string')]
protected $email;
/**
* @var array
*/
#[ORM\Column(type: 'json')]
protected $roles;
/**
* @var Post
*/
#[ORM\OneToMany(targetEntity: 'App\Entity\Post', mappedBy: 'user', fetch: 'LAZY')]
#[Ignore]
private Collection $posts;
I hope this help someone.我希望这对某人有所帮助。 Cheers!干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.