简体   繁体   English

如何处理教义和关系?

[英]Working with doctrine and relations, how?

I'm trying to make a method for activate users through a token, but i'm a little lost about how can i verify using doctrine 2 and relations.我正在尝试创建一种通过令牌激活用户的方法,但我对如何使用学说 2 和关系进行验证有点迷茫。

Here you can see an screen of my database relations.在这里您可以看到我的数据库关系的屏幕。

在此处输入图片说明

This is my user entity这是我的用户实体

.............
/**
     * @ORM\OneToOne(targetEntity="App\Entity\Token", mappedBy="username", cascade={"persist", "remove"})
     */
    private $token;

    public function getToken(): ?Token
    {
        return $this->token;
    }

    public function setToken(?Token $token): self
    {
        $this->token = $token;

        // set (or unset) the owning side of the relation if necessary
        $newUsername = $token === null ? null : $this;
        if ($newUsername !== $token->getUsername()) {
            $token->setUsername($newUsername);
        }

        return $this;
    }
.............

This is my token entity这是我的令牌实体

.............
/**
     * @ORM\OneToOne(targetEntity="App\Entity\User", inversedBy="token", cascade={"persist", "remove"})
     */
    private $username;

    public function getUsername(): ?User
    {
        return $this->username;
    }

    public function setUsername(?User $username): self
    {
        $this->username = $username;

        return $this;
    }
.............

The application works as expected when i register any user and the email has been sent.当我注册任何用户并且电子邮件已发送时,该应用程序按预期工作。

In other project without realtions in the token ( saving the token in the same table as user ) i didn't have any problem making the method as shown below:在令牌中没有 realtions 的其他项目中(将令牌保存在与用户相同的表中)我没有任何问题,使方法如下所示:

.............
/**
     * @Route("/activation/{token}/", name="activation/", methods={"GET"}))
     */
    public function activation(Request $request, User $user, $token)
    {
        $token = $request->attributes->get('token');
        $update = $user->getToken();
        $user->setToken(null);
        $user->setActive(true);
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($user);
        $entityManager->flush();
        return $this->render(
            'emails/confirmation.html.twig');
    }
.............

But with this method i'm receiving the following error Unable to guess how to get a Doctrine instance from the request information for parameter.但是使用这种方法,我收到以下错误无法猜测如何从参数的请求信息中获取 Doctrine 实例。

Anyone have any idea?任何人有任何想法?

Thanks for your time.谢谢你的时间。

Kind regards.亲切的问候。

.............................. ……………………………………………………………………………………………………………………………………………………………………………………

EDIT1: ERROR: Unable to guess how to get a Doctrine instance from the request information for parameter "token". EDIT1:错误:无法猜测如何从参数“token”的请求信息中获取 Doctrine 实例。

CODE:代码:

    /**
    * @Route("/activation/{pledge}/", name="activation/", methods={"GET"}))
    */
    public function activation(Request $request, Token $token, $pledge)
    {
        $user = $pledge->getUsername();
        if ( null === $user) {

        }
        $user->setToken(null);
        $user->setActive(true);
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($user);
        $entityManager->flush();
        return $this->render(' emails/confirmation.html.twig');
    }
}

EDIT2: SOLUTION编辑2:解决方案

/**
    * @Route("/activation/{token}/", name="activation/", methods={"GET"}))
    */
    public function activation(Request $request, Token $pledge, $token)
    {
        $token = $request->attributes->get('token');
        $user = $pledge->getUsername();
        if ( null === $pledge) {

        }
        $pledge->setToken(null);
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($pledge);
        $entityManager->flush();
        return $this->render('emails/confirmation.html.twig');
    }

In your old setup the User entity could be matched by ParamConverter because, as you wrote, there was a token field in it.在您的旧设置中, User实体可以与 ParamConverter 匹配,因为正如您所写,其中有一个token字段。

Now the token field is a part of the Token entity, so what you can do is match the Token entity and get the user from it:现在token字段是Token实体的一部分,所以你可以做的是匹配Token实体并从中获取用户:

public function activation(Request $request, Token $token)
{
    $user = $token->getUsername();
    if (null === $user) {
        // Here handle the situation with the token already used to activate a user
    }
    $user->setToken(null);
    $user->setActive(true);
    $entityManager = $this->getDoctrine()->getManager();
    $entityManager->persist($user);
    $entityManager->flush();

    return $this->render( 'emails/confirmation.html.twig');

}

 ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM