0

I am new to symfony, I am trying to display an array of result into twig template. here is the issue..

Here's My entity

<?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;


    }
}

Controller

 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
            ));
    }

Repository

<?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();
    }
}

Now when I use the following in the template..

{% 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 %}

I get this error - Key "date" for array with keys "0, 1, 2, 3" does not exist in ATTERACFirstBundle:Default:allshifts.html.twig at line 10

but on var_dump() I am getting the entire array. and when i use this

{% block pageContent %}

    <h2>Create Roster</h2>   
                        <h5>These are all your shifts</h5> <br>
                        {{ shift.0 }}
                        {{ shift.1 }}
                        {{ shift.2 }}
                        {{ shift.3 }}

 {% endblock %}

I am getting the only the site_name results.

These are all your shifts
QUT Gardens point QUT kelvin grove milton station Melbourne

I am not sure where i am going wrong. looking forward for insights. Thanks in advance. Cheers

1
  • Your __toString method will only return site_name so if you want to display first element od shift it wille be site_name. If you want more informatiob chain them with dots like Commented May 14, 2015 at 8:04

1 Answer 1

2
{% for shiftObject in shift %} {{ shiftObject.example }} {% endfor %}
Sign up to request clarification or add additional context in comments.

1 Comment

Damn.. thanks.. am such a noob.. lol Use "for" loop.. so simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.