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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示