简体   繁体   English

仅显示等于Symfony2中当前用户ID的结果

[英]Show only results equal to current user id in Symfony2

I have a Gecko bundle that in the index.html.twig file it loops through and shows all geckos in the database, which is working great, no problem. 我有一个Gecko包,该包在index.html.twig文件中循环通过,并显示数据库中的所有壁虎,效果很好,没有问题。

What i want to do however, is ONLY show geckos that have the same user id linked to them that's equal to the current user's id. 但是,我只想显示壁虎,它们具有链接到它们的相同用户ID等于当前用户的ID。

The index file is as follows: 索引文件如下:

{% extends '::base.html.twig' %}

{% block body -%}
    <h1>Gecko list</h1>

    <table class="records_list">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Aquisitiondate</th>
                <th>Morph</th>
                <th>Sex</th>
                <th>Genetics</th>
                <th>Bio</th>
                <th>Bred</th>
                <th>Hatchling</th>
                <th>Clutch</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
        {% for entity in entities %}
            <tr>
                <td><a href="{{ path('gecko_show', { 'name': entity.name }) }}">{{ entity.name }}</a></td>
                <td>{{ entity.name }}</td>
                <td>{% if entity.aquisitionDate %}{{ entity.aquisitionDate|date('Y-m-d H:i:s') }}{% endif %}</td>
                <td>{{ entity.morph }}</td>
                <td>{{ entity.sex }}</td>
                <td>{{ entity.genetics }}</td>
                <td>{{ entity.bio }}</td>
                <td>{{ entity.bred }}</td>
                <td>{{ entity.hatchling }}</td>
                <td>{{ entity.clutch }}</td>
                <td>
                <ul>
                    <li>
                        <a href="{{ path('gecko_show', { 'name': entity.name }) }}">show</a>
                    </li>
                    <li>
                        <a href="{{ path('gecko_edit', { 'name': entity.name }) }}">edit</a>
                    </li>
                </ul>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>

        <ul>
        <li>
            <a href="{{ path('gecko_new') }}">
                Create a new entry
            </a>
        </li>
    </ul>
    {% endblock %}

How do i go about limiting the view? 如何限制视野?

EDIT: 编辑:

This is the Gecko controller: 这是Gecko控制器:

<?php

namespace Breedr\GeckoBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Breedr\GeckoBundle\Entity\Gecko;
use Breedr\GeckoBundle\Form\GeckoType;

/**
 * Gecko controller.
 *
 * @Route("/gecko")
 */
class GeckoController extends Controller
{

    /**
     * Lists all Gecko entities.
     *
     * @Route("/", name="gecko")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('BreedrGeckoBundle:Gecko')->findAll();

        return array(
            'entities' => $entities,
        );
    }
    /**
     * Creates a new Gecko entity.
     *
     * @Route("/", name="gecko_create")
     * @Method("POST")
     * @Template("BreedrGeckoBundle:Gecko:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $entity = new Gecko();
        $entity->setUser($this->getUser());
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('gecko_show', array('name' => $entity->getName())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * Creates a form to create a Gecko entity.
     *
     * @param Gecko $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createCreateForm(Gecko $entity)
    {
        $form = $this->createForm(new GeckoType(), $entity, array(
            'action' => $this->generateUrl('gecko_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

    /**
     * Displays a form to create a new Gecko entity.
     *
     * @Route("/new", name="gecko_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new Gecko();
        $form   = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * Finds and displays a Gecko entity.
     *
     * @Route("/{name}", name="gecko_show")
     * @Method("GET")
     * @Template()
     */
    public function showAction($name)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('BreedrGeckoBundle:Gecko')->findOneByName($name);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Gecko entity.');
        }

        $deleteForm = $this->createDeleteForm($name);

        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
     * Displays a form to edit an existing Gecko entity.
     *
     * @Route("/{name}/edit", name="gecko_edit")
     * @Method("GET")
     * @Template()
     */
    public function editAction($name)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('BreedrGeckoBundle:Gecko')->findOneByName($name);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Gecko entity.');
        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($name);

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
    * Creates a form to edit a Gecko entity.
    *
    * @param Gecko $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(Gecko $entity)
    {
        $form = $this->createForm(new GeckoType(), $entity, array(
            'action' => $this->generateUrl('gecko_update', array('name' => $entity->getName())),
            'method' => 'PUT',
        ));

        $form->add('submit', 'submit', array('label' => 'Update'));

        return $form;
    }
    /**
     * Edits an existing Gecko entity.
     *
     * @Route("/{name}", name="gecko_update")
     * @Method("PUT")
     * @Template("BreedrGeckoBundle:Gecko:edit.html.twig")
     */
    public function updateAction(Request $request, $name)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('BreedrGeckoBundle:Gecko')->findOneByName($name);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Gecko entity.');
        }

        $deleteForm = $this->createDeleteForm($name);
        $editForm = $this->createEditForm($entity);
        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $em->flush();

            return $this->redirect($this->generateUrl('gecko_edit', array('name' => $name)));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }
    /**
     * Deletes a Gecko entity.
     *
     * @Route("/{name}", name="gecko_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, $name)
    {
        $form = $this->createDeleteForm($name);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('BreedrGeckoBundle:Gecko')->findOneByName($name);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Gecko entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('gecko'));
    }

    /**
     * Creates a form to delete a Gecko entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($name)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('gecko_delete', array('name' => $name)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }
}

EDIT 2: 编辑2:

This is from the Gecko entity: 这来自Gecko实体:

    /**
     * @ORM\ManyToOne(targetEntity="Breedr\UserBundle\Entity\User", inversedBy="geckos")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;
class GeckoController extends Controller
{

    /**
     * Lists all Gecko entities.
     *
     * @Route("/", name="gecko")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();
        $user = $this->getUser();
        $entities = $em->getRepository('BreedrGeckoBundle:Gecko')->findBy('user' => $user->getId());

        return array(
            'entities' => $entities,
        );
    }

We have to get user before View, so we can get it by 我们必须先获得用户,然后才能获得View

$user = $this->getUser();

Described here 在这里描述

Getting the user id from logged in user in FOSUserBundle 从FOSUserBundle中的登录用户获取用户ID

Next step is to get data from repository by user object 下一步是按用户对象从存储库中获取数据

By user object i preffer 通过用户对象我喜欢

$entities = $em
        ->getRepository('BreedrGeckoBundle:Gecko')
        ->findBy(array('user' => $user), array()
    );

I think it should work. 我认为应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM