php - 在 Symfony 中显示实体?
问题描述
我的任务是制作轮班管理网络应用程序。
我使用过 OpenSkedge(一个开源网络应用程序)。
我的任务之一是每个用户都应该拥有一套技能。
我已经添加了一个功能,您可以在其中添加这些技能集,并且我还有另一个视图可以为用户显示信息。
用户控制器:
/**
* Finds and displays a User entity.
*
* @param integer $id ID of user
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function viewAction($id)
{
$em = $this->getDoctrine()->getManager();
if (is_null($id)) {
$id = $this->getUser()->getId();
}
$entity = $em->getRepository('OpenSkedgeBundle:User')->find($id);
VarDumper::dump($entity);
if (!$entity instanceof User) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$deleteForm = $this->createDeleteForm($id);
return $this->render('OpenSkedgeBundle:User:view.html.twig', array(
'entity' => $entity,
'skillset' => $entity->getSkillset(),
'delete_form' => $deleteForm->createView(),
));
}
技能组实体:
<?php
// src/OpenSkedge/AppBundle/Entity/SkillSet.php
namespace OpenSkedge\AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\KeyValueStore\Mapping\Annotations as KeyValue;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* OpenSkedge\AppBundle\Entity\SkillSet
*
* @ORM\Table(name="os_skillset")
* @ORM\Entity
*/
class SkillSet
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=100)
*/
private $skillSet;
/**
* @ORM\Column(type="string", length=100)
*/
private $skillNumber;
/**
* @ORM\ManyToOne(targetEntity="user", inversedBy="sset")
* @ORM\JoinColumn(name="id", referencedColumnName="id")
*/
private $user;
public function getSkillSet()
{
return $this->skillSet;
}
public function setSkillSet($s){
$this->skillSet = $s;
return $this;
}
public function getSkillnumber()
{
return $this->skillNumber;
}
public function setSkillnumber($nm){
$this->skillNumber = $nm;
return $this;
}
public function getUser(){
return $this->user;
}
public function setUser(\OpenSkedge\AppBundle\Entity\User $u = null){
$this->user = $u;
return $this;
}
}
查看表格:
{% extends 'OpenSkedgeBundle:Dashboard:index.html.twig' %}
{% block title %}{{ app_brand_name() }} - {{ entity.name }}{% endblock %}
{% block modulecontent %}
<div class="span12">
<h3>{{ entity.name }}</h3>
{% if app.user.id == entity.id %}
<div class="btn-group header-control">
<a class="btn" href="{{ path('user_edit', { 'id': entity.id }) }}"><i class="icon-pencil"></i> Edit</a>
</div>
{% elseif is_granted('ROLE_ADMIN') %}
<form action="{{ path('user_delete', { 'id': entity.id }) }}" method="post">
<div class="btn-group header-control">
<a class="btn" href="{{ path('user_edit', { 'id': entity.id }) }}"><i class="icon-pencil"></i> Edit</a>
{% if app.user.id != entity.id and delete_form is defined %}
{{ form_widget(delete_form) }}
<button class="btn btn-danger" type="submit"><i class="icon-trash icon-white"></i> Delete</button>
{% endif %}
</div>
</form>
{% endif %}
<div class="btn-group header-control">
<a class="btn btn-info" href="{{ path('user_schedules', { 'id': entity.id }) }}">Schedules</a>
<a class="btn btn-info" href="{{ path('user_positions', { 'id': entity.id }) }}">Positions</a>
<a class="btn btn-info" href="{{ path('user_supervisors', { 'id': entity.id }) }}">Supervisors</a>
<a class="btn btn-info" href="{{ path('user_employees', { 'id': entity.id }) }}">Employees</a>
</div>
<table class="table table-condensed">
<tbody>
<tr>
<th>Username</th>
<td>{{ entity.username }}</td>
</tr>
<tr>
<th>Name</th>
<td>{{ entity.name }}</td>
</tr>
<tr>
<th>Role</th>
<td>{{ entity.group.name }}</td>
</tr>
{% if entity.workphone %}
<tr>
<th>Work Phone</th>
<td>{{ entity.workphone }}</td>
</tr>
{% endif %}
{% if entity.homephone %}
<tr>
<th>Home Phone</th>
<td>{{ entity.homephone }}</td>
</tr>
{% endif %}
{% if entity.location %}
<tr>
<th>Location</th>
<td>{{ entity.location }}</td>
</tr>
{% endif %}
<tr>
<th>Email</th>
<td>{{ entity.email }}</td>
</tr>
{% if entity.min %}
<tr>
<th>Minimum Hours</th>
<td>{{ entity.min }}</td>
</tr>
{% endif %}
{% if entity.max %}
<tr>
<th>Maximum Hours</th>
<td>{{ entity.max }}</td>
</tr>
{% endif %}
{% if entity.hours %}
<tr>
<th>Requested Hours</th>
<td>{{ entity.hours }}</td>
</tr>
{% endif %}
{% if entity.notes %}
<tr>
<th>Notes</th>
<td>{{ entity.notes }}</td>
</tr>
{% endif %}
{% if is_granted('ROLE_ADMIN') %}
{% if entity.supnotes %}
<tr>
<th>Supervisor Notes</th>
<td>{{ entity.supnotes }}</td>
</tr>
{% endif %}
{% endif %}
<tr>
<th>Color</th>
<td><span class="label" style="background-color: {{ entity.color }};">{{ entity.color }}</span></td>
</tr>
<tr>
<th>Active</th>
<td>{% if entity.isactive == 1 %}yes{% else %}no{% endif %}</td>
</tr>
</tbody>
</table>
<h3>Skillsets</h3>
<ul class="skillset">
{% for set in skillset %}
{% endfor %}
</ul>
</div>
{% endblock %}
下面显示了一个用户实体的转储:
如您所见,用户有一个 set 属性,其中包括已添加到新表单中的技能集。
但是,以下内容不会在页面中显示此类技能集:
<h3>Skillsets</h3>
<ul class="skillset">
{% for set in skillset %}
{% endfor %}
</ul>
我怎么解决这个问题?
解决方案
推荐阅读
- c# - 为什么 Process.StandardOutput 与实际输出不同和/或不完整?
- python - 使用基于多个字段的 If/Else 语句重新分类“添加字段”
- c# - 下载具有 Knockout 绑定、ASP.NET MVC 堆栈且没有 Razor 的文件
- python - 如何从字符串中删除特定数字?
- python - 使用组合模式会导致 Django Factory Boy 的无限递归?
- autodesk-forge - 将 DWG 转换为 DWF - 了解 externalid 属性
- python - 在 AWS lambda 中组织外部依赖项
- javascript - 如何将 python 脚本转换为 Web 应用程序?
- r - 如何在数据框中修剪部分列名(TCGA 患者 ID)
- fetch - 如何制作一个网络脚本来从 Wikipedia 或其他地方检索 1,800 个模型的汽车模型年份?