我是Symfony的新手,我试图将结果数组显示为Twig模板。这是问题。
这是我的实体
<?php
namespace XYZFirstBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Shifts
* @ORMEntity(repositoryClass="XYZFirstBundleRepositoryShiftRepository")
* @ORMTable(name="shifts")
*/
class Shifts
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var DateTime
*
* @ORMColumn(name="date", type="date")
*/
private $date;
/**
* @var string
*
* @ORMColumn(name="site_name", type="string", length=255)
*/
private $site_name;
/**
* @var string
*
* @ORMColumn(name="location", type="string", length=255)
*/
private $location;
/**
* @var DateTime
*
* @ORMColumn(name="start_time", type="time")
*/
private $startTime;
/**
* @var DateTime
*
* @ORMColumn(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 XYZFirstBundleRepository;
use DoctrineORMEntityRepository;
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 %}
我有这个错误 - 键的"日期"与键的数组" 0、1、2、3"在atteracfirstbundle中不存在:默认值:allshifts.html.twig在第10行
但是在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 Point Qut Kelvin Grove Milton Station Melbourne
我不确定我要去哪里出错。期待见解。提前致谢。欢呼
{% for shiftObject in shift %} {{ shiftObject.example }} {% endfor %}