我从数据库中获取所有用户时使用Symfony 2收到此错误:
Variable "name" does not exist in UsersListBundle:Default:index.html.twig at line 1
500 Internal Server Error - Twig_Error_Runtime
我的代码:index.html.twig
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Symfony 2 Example</title>
<link href="/bundles/userslist/css/main.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<div id="container">
<h1>User list page</h1>
<div id="body">
<p><a href="/">Click here</a> to back to the Homepage</p>
<p><a href="users/add">ADD NEW USER</a></p>
<p>All users in the users table using: <br />
<strong>$repository = $this->getDoctrine()->getRepository('UsersListBundle:Users');<br />
$users = $repository->findAll();</strong></p>
{% if users is not empty %)
<table cellpadding="4">
<tr>
<td><strong>ID</strong></td>
<td><strong>Name</strong></td>
<td><strong>Email</strong></td>
<td><strong>Phone</strong></td>
</tr>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.phone }}</td>
<td><a href="users/edit/{{ user.id }}">Edit</a></td>
<td><a href="users/delete/{{ user.id }}">Delete</a></td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No users in the database</p>
{% endif %}
<p>Get ID 1 in the example table using: <br />
<strong>$user = $this->getDoctrine()->getRepository('UsersListBundle:Users')->find(1);</strong></p>
</div>
</div>
</body>
</html>
这是我的控制器代码:
/**
* @Route("/users")
* @Template()
*/
public function indexAction()
{
$repository = $this->getDoctrine()
->getRepository('UsersListBundle:Users');
$users = $repository->findAll();
return array('users' => $users);
}
我已经阅读了一些关于此错误的文章。他们说一个隐藏的角色。点击Alt Gr + Space时会出现此字符。 现在我不知道如何删除该角色。我尝试重新输入相同的代码,但没有运气。
这是我的用户实体:
<?php
// src/Users/Bundle/ListBundle/Entity/Users.php
namespace Users\Bundle\ListBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class Users
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*/
protected $name;
/**
* @ORM\Column(type="string", length=200)
*/
protected $email;
/**
* @ORM\Column(type="string", length=60)
*/
protected $phone;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Users
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set email
*
* @param string $email
* @return Users
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set phone
*
* @param string $phone
* @return Users
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
}
这是我的UserController:
<?php
namespace Users\Bundle\ListBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Users\Bundle\ListBundle\Entity\Users;
use Symfony\Component\HttpFoundation\Request;
class UsersController extends Controller
{
/**
* @Route("/users")
* @Template()
*/
public function indexAction()
{
$repository = $this->getDoctrine()
->getRepository('UsersListBundle:Users');
$users = $repository->findAll();
return array('users' => $users);
}
/**
* @Route("/users/add")
* @Template()
*/
public function addAction(Request $request)
{
$user= new Users();
$user->setName('Name');
$user->setEmail('Email');
$user->setPhone('Phone');
$form=$this->createFormBuilder($user)
->add('name')
->add('email')
->add('phone')
->getForm();
if ($request->getMethod()=='POST'){
$form->bind($request);
if ($form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirect($this->generateUrl('users_list_users_index'), 301);
}
} else {
return $this->render('UsersListBundle:Users:add.html.twig',array(
'form'=>$form->createView(),
));
}
}
/**
* @Route("/users/edit/{id}", requirements={"id" = "\d+"}, defaults={"id" = 0})
* @Template()
*/
public function editAction($id, Request $request)
{
if ($id == 0){ // no user id entered
return $this->redirect($this->generateUrl('users_list_users_index'), 301);
}
$user = $this->getDoctrine()
->getRepository('UsersListBundle:Users')
->find($id);
if (!$user) {// no user in the system
throw $this->createNotFoundException(
'No user found for id '.$id
);
}
$form=$this->createFormBuilder($user)
->add('name')
->add('email')
->add('phone')
->getForm();
if ($request->getMethod()=='POST'){
$form->bind($request);
if ($form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirect($this->generateUrl('users_list_users_index'), 301);
}
} else {
return $this->render('UsersListBundle:Users:edit.html.twig',array(
'form'=>$form->createView(),
));
}
return array('name' => $id);
}
/**
* @Route("/users/delete/{id}", requirements={"id" = "\d+"}, defaults={"id" = 0})
* @Template()
*/
public function deleteAction($id)
{
if ($id == 0){ // no user id entered
return $this->redirect($this->generateUrl('users_list_users_index'), 301);
}
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('UsersListBundle:Users')->find($id);
if (!$user) { // no user in the system
throw $this->createNotFoundException(
'No user found for id '.$id
);
} else {
$em->remove($user);
$em->flush();
return $this->redirect($this->generateUrl('users_list_users_index'), 301);
}
}
}
答案 0 :(得分:1)
这一行有一个语法问题:
{% if users is not empty %)
将%} 替换为%}
答案 1 :(得分:0)
也许没有这样的字段名称。 这是第1行的错误:
<!DOCTYPE html>
或在:
<td>{{ user.name }}</td>
尝试删除第二个并检查是否仍有错误。
你能告诉我用户的twig转储或UsersListBundle:用户实体
.....
{% if users is not empty %)
{% dump(users) %}
.....
答案 2 :(得分:0)
如果您在数据库中添加或重命名了“名称”字段,则必须重新生成您的实体。将“用户”实体添加到您的问题将有助于您获得更快的响应。