我有一个Gecko捆绑包,它在index.html.twig
文件中循环显示数据库中的所有壁虎,效果很好,没问题。
我想要做的只是显示与他们相同的用户ID的壁虎,它们等于当前用户的ID。
索引文件如下:
{% 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 %}
我如何限制观点?
编辑:
这是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()
;
}
}
编辑2:
这来自Gecko
实体:
/**
* @ORM\ManyToOne(targetEntity="Breedr\UserBundle\Entity\User", inversedBy="geckos")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
答案 0 :(得分:3)
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,
);
}
答案 1 :(得分:2)
我们必须在View之前获取用户,因此我们可以通过
获取它$user = $this->getUser();
这里描述
{{3}}
下一步是按用户对象
从存储库中获取数据按用户对象我预先
$entities = $em
->getRepository('BreedrGeckoBundle:Gecko')
->findBy(array('user' => $user), array()
);
我认为它应该有用。