我的例子很简单。我有用户。他们可以提交提示。其他人可以信用 支持这些想法。当用户想要支持一个想法时,我需要检查他是否有足够的信用等等。我在哪里放这个代码?
/**
* @Route("/support/{ideaSlug}", name="support_new")
*/
public function newAction(Request $request, $ideaSlug)
{
$support = new Support();
$form = $this->createForm(SupportType::class, $support);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $this->getUser();
if($user->getCredit() > 0) {
$idea = $this->getDoctrine()->getRepository('AppBundle:Idea')->findOneBy(['slug' => $ideaSlug]);
$em = $this->getDoctrine()->getManager();
$em->persist($support);
$em->flush();
}
return $this->redirect($this->generateUrl('support_index'));
}
return $this->render('support/new.html.twig', [
'form' => $form->createView()
]);
}
我是否需要用户服务,例如:
namespace AppBundle\Utils;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use AppBundle\Entity\Idea;
class User
{
protected $em;
protected $user;
public function __construct(EntityManager $em, TokenStorage $tokenStorage)
{
$this->em = $em;
$this->$user = $tokenStorage->getToken()->getUser();
}
public support(Idea $idea)
{
....
}
}
或者我将它放在实体类中?