在 Symfony 中向用户配置文件添加自定义数据

Adding custom data to User Profile in Symfony

我正在做小型 Symfony 项目。除了它包含用户授权和配置文件的所有内容。我为此需求添加了 FOSUserBundle,顺便说一句,它开箱即用。

这是我的show_content.html.twig,实际上它和盒子里的几乎一样:

{% 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>

每个用户都可以获得积分。我为它创建了简单的实体:

<?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
    }

我还添加了一个简单的表单,用户可以在其中按下一个按钮,随机生成的积分就会进入他的帐户。它运行良好,因为它只是 User 实体中的一个变量。但是现在我需要在用户配置文件中实现点数获取历史记录。这就是创建 Points 实体的原因。我看到积分是 运行 fos_user_points table,但我如何将这些内容添加到用户个人资料中?我不确定直接从数据库中获取它们是否安全。

如果我理解得很好,积分与创建它们的用户有关。要实现这一点,您应该创建从 User 实体到 Points 实体的一对多关系。首先,您应该编写自己的用户实体,该实体继承自 FOSUserBundle 提供的实体。

例如:

<?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;

顺便问一下,Points 实体中的 email 属性有什么用?如果是为了处理积分和用户的关系,那就没必要了

好的,我刚刚这样解决了我的问题:

我已经从 ProfileController.php 覆盖了 showAction(),像这样:

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
        ));
    }

show_content.html.twig像这样:

{% 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>

现在可以使用了!