Php 在Symfony中将自定义数据添加到用户配置文件
我在做一个小型的Symfony项目。除此之外,它还包含用户授权和配置文件。我添加了FOSUserBundle以满足这一需求,顺便说一句,它在盒子里工作得非常好 这是我的Php 在Symfony中将自定义数据添加到用户配置文件,php,symfony,twig,Php,Symfony,Twig,我在做一个小型的Symfony项目。除此之外,它还包含用户授权和配置文件。我添加了FOSUserBundle以满足这一需求,顺便说一句,它在盒子里工作得非常好 这是我的show\u content.html.twig,实际上它与框中的内容几乎相同: {% trans_default_domain 'FOSUserBundle' %} <div class="fos_user_user_show"> <p>{{ 'profile.show.email'|trans
show\u 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>
{%trans\u默认\u域'FOSUserBundle'%}
{{'profile.show.email'{trans}}:{{{user.email}
{{'profile.show.points'{trans}}:{{{user.points}
每个用户都可以获得积分。我为它创建了简单实体:
<?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
}
如果我理解得很好,这些点与创建它们的用户相关。要实现这一点,您应该创建从用户实体到点实体的一对多关系。首先,您应该编写自己的用户实体,该实体继承了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;
好的,我只是这样解决了我的问题:
我从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
));
}
{% 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>
并按如下方式显示_content.html.twig:
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
));
}
{% 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>
{%trans\u默认\u域'FOSUserBundle'%}
{{'profile.show.email'{trans}}:{{{user.email}
{{'profile.show.points'{trans}}:{{{user.points}
积分历史:
{点中的点的百分比%}
{{point.datetime}
{{point.points}}}点
{%endfor%}
现在它工作了 谢谢你的回复!我采取了另一种方式来实现我想要的。我已经从ProfileController中重写了showAction()
,并且刚刚将点作为变量添加到$this->render
数组中。但现在我一直在Points类中编写getPoints
函数。你认为它将如何工作?或者更好地利用你所提供的?很高兴知道它有效。但我仍然认为您应该在模型中实现用户和点之间的关系,IMHO将更加健壮、简单和易于维护。在User
类中有points
属性,在points
类中有User
属性。有关详细说明,请参阅Symfony手册中关于的部分。谢谢!我一定会尝试你的解决方案。实际上,你可以删除!是对象($user)