Symfony 4的”Match”方法类似于Tinder。

huangapple go评论73阅读模式
英文:

Symfony 4 Match method like tinder

问题

I work on a website for my last school project, it's a website where (as a normal user) you can match with a coach and once you two have matched together, you can share a page where the coach can update your daily workout routine.

I'm stucked at the matching method, my entity User has a propriety idCoach which is an array that can contains other Users. The idea is when you are on the coach's profile page, and you press on the match button, it adds the coach on your idCoach propriety.

This is my property from my User entity:

/**
 * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="idSportif")
 */
private $idCoach;

This is what I tried on the controller:

/**
 * @Route("/user/match/{id}", name="match")
 */
public function matchCoach(ObjectManager $om, User $user) {
    $currentUser = $this->getUser();
    $currentUser->addIdCoach($user);
    $om->persist($currentUser);
    $om->flush();

    return $this->render('profile/index.html.twig', [
        'user' => $currentUser,
    ]); 
}

But it gives me an error message:

Call to a member function addIdCoach() on null.

英文:

I work on a website for my last school project, it's a website where (as a normal user) you can match with a coach and once you two have matched together, you can share a page where the coach can update your daily workout routine.

I'm stucked at the matching method, my entity User has a propriety idCoach which is an array that can contains other Users. The idea is when you are on the coach's profile page, and you press on the match button, it adds the coach on your idCoach propriety.
This is my propriery from my User entity:

/**
 * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="idSportif")
 */
private $idCoach;

this is what i tried on the controller :

/**     
 * @Route("/user/match/{id}", name="match")
 */
public function matchCoach(ObjectManager $om, User $user) {
    $currentUser = $this->getUser();
    $currentUser->addIdCoach($user);
    $om->persist($currentUser);
    $om->flush();

    return $this->render('profile/index.html.twig', [
        'user' => $currentUser,
    ]); 
        
}

but it gives me an error message :
> Call to a member function addIdCoach() on null.

答案1

得分: 1

getUser()返回null,这就是你收到错误的原因。

你的控制器是否扩展了AbstractController,并且getUser()应该返回当前登录的用户?在这种情况下,看起来你没有登录。我建议检查安全配置,确保只有已登录的用户可以访问这个URL。

进一步的评论:

  • 你的命名有点混淆,"idCoach"是什么意思?如果这个属性保存了关联的教练,我会将其命名为"coaches"。
  • 你的方法更改了数据,所以应该只能通过POST调用,而不是GET。
英文:

so getUser() returns null, that's why you get the error.

Does your Controller extend AbstractController and getUser() should therefore return the currently logged in user? In this case, it seems like you are not logged in. I would check the security configuration to make sure only logged in users can access this url.

Further comments:

  • Your naming is a little confusing, what is an "idCoach"? If this property holds the associated coaches, I would name it "coaches".
  • Your method changes data, so it should only be called via POST, not GET.

huangapple
  • 本文由 发表于 2020年1月6日 18:32:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定