symfony2中的详细信息列表,来自两个捆绑包

时间:2017-01-24 15:36:12

标签: symfony listview

我对Symfony的开发很少,我有一个问题:要显示包含两个Bundles详细信息的列表。

我的项目有一个所有者,但可以有1个以上的顾问。实体已经相关但是当列出结果时,只能查看用户ID。怎么看用户名?

我将附加我的实体图像。

谢谢, View Entities

1 个答案:

答案 0 :(得分:0)

我需要在项目中显示不同顾问的名称。这些也是用户。我已经与该项目的所有者有关,但该顾问是我实体的一个字段。

附件图片参考,以及php和twig文件内容。查看图片:it should go looking for your gcloud credentials

UserProjectController

public function showAction(UserProject $userProject, User $user) {
    $id      = $userProject->getId();
    $em      = $this->getDoctrine()->getManager();
    $project = $em->getRepository('ProjectBundle:Project')->find($id);
    $userProjects = $em->getRepository('ProjectBundle:UserProject')->findby(array('idproject' => $id, 'status' => '1'));
    if (empty($userProjects)) {
        return $this->redirectToRoute('userproject_new');
    }
    $users = $em->getRepository('UserBundle:User')->findby(array('id' => 1));

    return $this->render('ProjectBundle:Userproject:show.html.twig', array(
            'userProjects' => $userProjects, 'project' => $project, 'User' => $user,
        ));
}

实体用户

/**
 * @ORM\OneToMany(targetEntity="ProjectBundle\Entity\UserProject", mappedBy="user")
 */
protected $uproject;

实体UserProject

/**
 * @ORM\ManyToOne(targetEntity="ProjectBundle\Entity\Project", inversedBy="userproject")
 * @ORM\JoinColumn(name="project_id", referencedColumnName="id")
 * @Assert\NotBlank()
 */
protected $idproject;
/**
 * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="uproject")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 * @Assert\NotBlank()
 */
protected $user;

实体项目

/**
 * @ORM\OneToMany(targetEntity="ProjectBundle\Entity\UserProject", mappedBy="idproject")
 */
protected $userproject;

查看show.html.twig

{% for userProject in userProjects %}
    <br>
    <dl>
    <dt><span class="text-primary">{{'User_id'|trans}}</span></dt>
        <dd>
            {{ userProject.user }}
            &nbsp;
        </dd>
        <br>
        <dt><span class="text-primary">{{'Consultor_id'|trans}}</span></dt>
        <dd>
            {{ userProject.consultorId }}
            &nbsp;
        </dd>
        <br>
        <dt><span class="text-primary">{{'Status'|trans}}</span></dt>
        <dd>
            {% if userProject.status == 1 %}
                <span class="text-success">{% trans %}Enabled{% endtrans %}</span>
            {% elseif userProject.status == 0 %}
                <span class="text-danger">{% trans %}Disabled{% endtrans %}</span>
            {% endif %}
            &nbsp;
        </dd>
        <br>
    </dl>
{% endfor %}