首页 > 解决方案 > 在 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>

我怎么解决这个问题?

标签: phpsymfony

解决方案


推荐阅读