I'm working on small Symfony project. Besides everything it contains User authorization and profile. I added FOSUserBundle for this needs, it works great from the box btw.
Here is my show_content.html.twig
, actually it's almost the same as it goes from the box:
{% trans_default_domain 'FOSUserBundle' %}
<div class="fos_user_user_show">
<p>{{ 'profile.show.email'|trans }}: {{ user.email }}</p>
<p>{{ 'profile.show.points'|trans }}: {{ user.points }}</p>
<p><a href="{{ path('fos_user_security_logout') }}">Logout</a></p>
</div>
Each user can earn points. I created simple entity for it:
<?php
namespace Acme\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user_points")
*/
class Points
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $datetime;
/**
* @ORM\Column(type="integer")
*/
protected $points;
/**
* @ORM\Column(type="string")
*/
protected $email;
//Getters and setters go here
}
I also added a simple form where a user can press a button and randomly generated points goes to his account. It worked well, as it was just was one more variable in a User entity. But now I need to implement points getting history in a user profile. This why Points entity was created. I see points are running to fos_user_points table, but how do I put this content to the User profile? I'm not sure if it's safe to directly grab 'em from a database.
If I understand it well, the Points are related to the User that created them. To implement this, you should create a one-to-many relation from your User entity to the Points entity. First you should write your own User entity that inherits from the one provided by FOSUserBundle.
For example:
<?php
namespace Acme\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
/**
* User
*
* @ORM\Table(name="fos_user")
* @ORM\Entity(repositoryClass="Acme\UserBundle\Entity\UserRepository")
*/
class User extends BaseUser
{
/**
* @var integer
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Acme\UserBundle\Entity\Points")
*/
protected $points;
By the way, what is the usage of the email
attribute in the Points
entity ? I f it was there to handle the relation between points and user, it isn't necessary anymore.
Ok, I just solved my problem this way:
I have overrode showAction()
from ProfileController.php like this:
public function showAction()
{
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$points = $em->getRepository('AcmeUserBundle:Points')->findBy(array('email' => $user->getEmail()));
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
return $this->render('FOSUserBundle:Profile:show.html.twig', array(
'user' => $user,
'points' => $points
));
}
and show_content.html.twig like this:
{% trans_default_domain 'FOSUserBundle' %}
<div class="fos_user_user_show">
<p>{{ 'profile.show.email'|trans }}: {{ user.email }}</p>
<p>{{ 'profile.show.points'|trans }}: {{ user.points }}</p>
<p>Points history:</p>
<table style="border: 1px">
{% for point in points %}
<tr>
<td>{{ point.datetime }}</td>
<td>{{ point.points }} points</td>
</tr>
{% endfor %}
</table>
<p><a href="{{ path('fos_user_security_logout') }}">Logout</a></p>
</div>
And now it works!