Php 在Symfony中显示实体?

Php 在Symfony中显示实体?,php,symfony,Php,Symfony,我的任务是转变管理web应用程序的方式 我使用过OpenSkedge(一个开源web应用程序) 我的任务之一是每个用户都应该有一套技能 我已经添加了一个功能,您可以在其中添加这些技能集,并且我还有另一个视图显示用户的信息 用户控制器: /** * Finds and displays a User entity. * * @param integer $id ID of user * * @return \Symfony\Component\HttpFoundation\Respons

我的任务是转变管理web应用程序的方式

我使用过OpenSkedge(一个开源web应用程序)

我的任务之一是每个用户都应该有一套技能

我已经添加了一个功能,您可以在其中添加这些技能集,并且我还有另一个视图显示用户的信息

用户控制器:

/**
 * 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 %}
技能集实体:

/**
 * 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 %}

我在屏幕截图中的用户类中没有看到属性
skillSet
?而且yor for循环为空。可能您希望执行以下操作:`{set.skillset%}{{set.skillset}}{%endfor%}`skillset是实体中的字符串。不能对字符串执行for。@codeneuss No skillset是skillset类中的字符串属性。我在第一次阅读时也落入了同样的陷阱。@dbrumann提到你忘了回应你的技能;)