2015-09-21 42 views
0

我有以下的控制器:为什么函数涉及不在函数中的代码?

<?php 

namespace Dev\TaskBundle\Controller; 

use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpFoundation\Request; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Session\Session; 
use Dev\TaskBundle\Entity\User; 

class UserController extends Controller 
{ 
    /** 
    * Redirects to directory where you can input new password 
    * 
    * @Route("/{id}", name="user_update") 
    */ 

    public function userUpdatePanel($id) 
    { 

     $user = $this->getDoctrine()->getRepository('DevTaskBundle:User')->find($id); 
     return $this->render('DevTaskBundle:User:userUpdate.html.twig', array('user' => $user)); 

    } 

    /** 
    * Updates password of the user 
    * 
    * @Route("/updatePassword", name="updatePass_action") 
    */ 


    public function passUpdateAction(Request $request) 
    { 

     $em = $this->getDoctrine()->getManager(); 
     $id = $request->get('id'); 
     $user = new User(); 
     $user = $em->getRepository('DevTaskBundle:User')->find($id); 
     $password = $request->get('passInput'); 
     $user->setPassword($password); 
     $em->flush(); 

     $users = $this->getDoctrine()->getRepository('DevTaskBundle:User')->findAll(); 
     return $this->render('DevTaskBundle:User:accounts.html.twig', array('users' => $users)); 

    } 

} 

这里是从形式userUpdate.html.twig

<form action="{{ path('updatePass_action') }}" method="POST"> 


<div> 
     Username: {{ user.username }} 
    </div> 

     <input type="hidden" name="id" value="{{user.id}}"> 
     <div class="form-group"> 
      <label for="passInput">Password:</label> 
      <input type="text" class="form-control" id="passInput" name="passInput" value="{{ user.password }}"> 
     </div> 
     <button type="submit" class="btn btn-default">Change</button> 
    </form> 

,当我继续更改密码我接收这样的错误

Impossible to access an attribute ("username") on a null variable in DevTaskBundle:User:userUpdate.html.twig at line 23 

更改密码后,我想呈现accounts.html.twig,为什么有关于userUpdate的错误?这里有什么问题? accounts.html.twig与树枝脚本

部分:

<table class="table table-bordered"> 
<thead> 
    <tr> 
     <th>Login</th> 
     <th>Hasło</th> 
     <th>Narzędzia</th> 
    </tr> 
</thead> 
<tbody> 
{% for user in users %} 
    <tr> 

     <td> 
      {{ user.username }} 
     </td> 

     <td> 
      {{ user.password }} 
     </td> 

     <td>   
      <a href="{{ path('user_update', {'id': user.id}) }}" > 
      <span class="glyphicon glyphicon-pencil linkDeco" ></span> 
      </a> 
     {% if user.status == 1 %}  
      <a href="{{ path('deleteAction', {'name': 'User' ,'direction': 'account_panel' , 'id': user.id}) }}" > 
      <span class="glyphicon glyphicon-trash linkDeco"></span> 
      </a> 
     {% endif %} 
     </td> 
    </tr> 
{% endfor %} 
</tbody> 
</table> 

编辑 ,因为当我删除

<div> 
      Username: {{ user.username }} 
     </div> 

问题必须与userUpdate.html.twig我有错误

Impossible to access an attribute ("id") on a null variable in DevTaskBundle:User:userUpdate.html.twig at line 24 

任何想法,这是为什么?

回答

0

Doctrine find方法返回对象数组,当您只需要一个结果时,使用findOne而不是find

$user = $this->getDoctrine()->getRepository('DevTaskBundle:User')->findOneById($id); 
相关问题