douzhangjian1505 2015-08-04 22:46
浏览 89
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • downloadTemp2014 2015-08-05 00:18
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀