我试图编辑我的indexAction函数,我想为用户
找到BYOnepublic function indexAction() {
$em = $this->getDoctrine()->getManager();
if ($this->isGranted('ROLE_ADMIN')) {
$leaveRequests = $em->getRepository('AppBundle:LeaveRequest')->findAll();
} else {
$leaveRequests = $em->getRepository('AppBundle:LeaveRequest')->findOneBy(['id' => $id]);
}
return $this->render('leaverequest/index.html.twig', array(
'leaveRequests' => $leaveRequests,
));
}
我收到以下错误
注意:未定义的变量:id
我看过实体,但没找到任何东西。 这是我的实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* LeaveRequest
*
* @ORM\Table(name="leave_request")
* @ORM\Entity(repositoryClass="AppBundle\Repository\LeaveRequestRepository")
*/
class LeaveRequest
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="start_date", type="date")
*/
private $startDate;
/**
* @var \DateTime
*
* @ORM\Column(name="end_date", type="date")
*/
private $endDate;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var int
*
* @ORM\Column(name="user_id", type="integer")
*/
private $userId;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="leaveRequests")
*
* @var \AppBundle\Entity\User;
*/
protected $user;
/**
* Set user
*
* @param \AppBundle\Entity\User $user
* @return LeaveRequest
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set startDate
*
* @param \DateTime $startDate
*
* @return LeaveRequest
*/
public function setStartDate($startDate)
{
$this->startDate = $startDate;
return $this;
}
/**
* Get startDate
*
* @return \DateTime
*/
public function getStartDate()
{
return $this->startDate;
}
/**
* Set endDate
*
* @param \DateTime $endDate
*
* @return LeaveRequest
*/
public function setEndDate($endDate)
{
$this->endDate = $endDate;
return $this;
}
/**
* Get endDate
*
* @return \DateTime
*/
public function getEndDate()
{
return $this->endDate;
}
/**
* Set type
*
* @param string $type
*
* @return LeaveRequest
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set description
*
* @param string $description
*
* @return LeaveRequest
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set userId
*
* @param integer $userId
*
* @return LeaveRequest
*/
public function setUserId($userId)
{
$this->userId = $userId;
return $this;
}
/**
* Get userId
*
* @return int
*/
public function getUserId()
{
return $this->userId;
}
/**
* Is the given User the author of this Post?
*
* @param User|null $user
* @return bool
*/
public function isUser(User $user = null)
{
return $user && $user->getId() == $this->getUserId();
}
}
编辑:从实体添加缺失的部分作为代码。在此先感谢任何人为此添加答案。
答案 0 :(得分:2)
您正在尝试使用未定义的变量(如通知所说)
以下是您可以做的一个示例:
public function indexAction() {
$user = $this->getUser();
$userId = $user->getId();
$em = $this->getDoctrine()->getManager();
if ($this->isGranted('ROLE_ADMIN')) {
$leaveRequests = $em->getRepository('AppBundle:LeaveRequest')->findAll();
} else {
// I admit that in your LeaveRequest entity you have a relation with your User Entity which name is 'userId'
$leaveRequests = $em->getRepository('AppBundle:LeaveRequest')->findOneBy(['userId' => $userId]);
}
return $this->render('leaverequest/index.html.twig', array(
'leaveRequests' => $leaveRequests,
));
}
此致