我是symfony的新手,我正在尝试将结果数组显示到twig模板中。这是问题..
这是我的实体
<?php
namespace XYZ\FirstBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Shifts
* @ORM\Entity(repositoryClass="XYZ\FirstBundle\Repository\ShiftRepository")
* @ORM\Table(name="shifts")
*/
class Shifts
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="date")
*/
private $date;
/**
* @var string
*
* @ORM\Column(name="site_name", type="string", length=255)
*/
private $site_name;
/**
* @var string
*
* @ORM\Column(name="location", type="string", length=255)
*/
private $location;
/**
* @var \DateTime
*
* @ORM\Column(name="start_time", type="time")
*/
private $startTime;
/**
* @var \DateTime
*
* @ORM\Column(name="end_time", type="time")
*/
private $endTime;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set date
*
* @param \DateTime $date
* @return Shifts
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set site_name
*
* @param string $location
* @return Shifts
*/
public function setSitename($site_name)
{
$this->site_name = $site_name;
return $this;
}
/**
* Get site_name
*
* @return string
*/
public function getSitename()
{
return $this->site_name;
}
/**
* Set location
*
* @param string $location
* @return Shifts
*/
public function setLocation($location)
{
$this->location = $location;
return $this;
}
/**
* Get location
*
* @return string
*/
public function getLocation()
{
return $this->location;
}
/**
* Set startTime
*
* @param \DateTime $startTime
* @return Shifts
*/
public function setStartTime($startTime)
{
$this->startTime = $startTime;
return $this;
}
/**
* Get startTime
*
* @return \DateTime
*/
public function getStartTime()
{
return $this->startTime;
}
/**
* Set endTime
*
* @param \DateTime $endTime
* @return Shifts
*/
public function setEndTime($endTime)
{
$this->endTime = $endTime;
return $this;
}
/**
* Get endTime
*
* @return \DateTime
*/
public function getEndTime()
{
return $this->endTime;
}
/**
*
* @return string String representation of this class
*/
public function __toString()
{
return (string) $this->site_name;
return (string) $this->location;
return (string) $this->startTime;
return (string) $this->endTime;
}
}
控制器
public function showShiftAction()
{
$em = $this->getDoctrine()->getManager();
$shift =array();
$shift = $em->getRepository('XYZFirstBundle:Shifts')
->findAll();
var_dump($shift);
return $this->render('XYZFirstBundle:Default:allshifts.html.twig', array(
'shift' => $shift
));
}
存储库
<?php
namespace XYZ\FirstBundle\Repository;
use Doctrine\ORM\EntityRepository;
class ShiftRepository extends EntityRepository
{
public function showAllShifts()
{
return $this->getEntityManager()
->createQuery (
'SELECT Shifts FROM XYZFirstBundle:Shifts Shifts'
)
->getResult();
}
}
现在我在模板中使用以下内容..
{% extends 'base.html.twig' %}
{% block title %}RMS-Roster{% endblock %}
{% block pageContent %}
<h2>Create Roster</h2>
<h5>These are all your shifts</h5> <br>
{{ shift.date }}
{{ shift.site_name }}
{{ shift.location }}
{{ shift.startTime }}
{{ shift.endTime }}
{% endblock %}
我收到此错误 - ATTERACFirstBundle中不存在具有键“0,1,2,3”的数组的键“日期”:默认:第10行的allshifts.html.twig
但在var_dump()
我正在获取整个数组。当我使用这个
{% block pageContent %}
<h2>Create Roster</h2>
<h5>These are all your shifts</h5> <br>
{{ shift.0 }}
{{ shift.1 }}
{{ shift.2 }}
{{ shift.3 }}
{% endblock %}
我获得了唯一的site_name结果。
这些都是你的轮班 QUT Gardens指向QUT开尔文格罗夫米尔顿站墨尔本
我不确定我哪里出错了。期待见解。 提前致谢。 干杯
答案 0 :(得分:2)
{% for shiftObject in shift %} {{ shiftObject.example }} {% endfor %}