Symfony3 - 显示时嵌入的集合表单为空

时间:2017-06-07 10:21:00

标签: forms symfony collections

我创建了一个14个时段的时间卡表单,每天一个时段。 我创建了实体,控制器和formType。在时间卡控制器的新操作中,我创建了14个dayslot实例,并使用实体函数将它们添加到时间卡中。表单显示为空 - 好像集合是空的。我还有什么需要做的吗?似乎表单没有正确绑定到实体集合对象。我在显示它之前已经抛弃了实体,似乎就在那里。

以下是TimeCard实体:

<?php

namespace CockpitBundle\Entity;

/**
 * TCDaySlot
 */
class TCDaySlot
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var \DateTime
     */
    private $date;

    /**
     * @var string
     */
    private $reghours;

    /**
     * @var string
     */
    private $othours;

    /**
     * @var string
     */
    private $holidayhours;

    /**
     * @var string
     */
    private $type1hours;

    /**
     * @var string
     */
    private $type2hours;

    /**
     * @var string
     */
    private $type3hours;

    /**
     * @var string
     */
    private $type4hours;

    /**
     * @var string
     */
    private $type5hours;

    /**
     * @var string
     */
    private $note;

    /**
     * @var \CockpitBundle\Entity\Employee
     */
    private $employee;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set date
     *
     * @param \DateTime $date
     *
     * @return TCDaySlot
     */
    public function setDate($date)
    {
        $this->date = $date;

        return $this;
    }

    /**
     * Get date
     *
     * @return \DateTime
     */
    public function getDate()
    {
        return $this->date;
    }

    /**
     * Set reghours
     *
     * @param string $reghours
     *
     * @return TCDaySlot
     */
    public function setReghours($reghours)
    {
        $this->reghours = $reghours;

        return $this;
    }

    /**
     * Get reghours
     *
     * @return string
     */
    public function getReghours()
    {
        return $this->reghours;
    }

    /**
     * Set othours
     *
     * @param string $othours
     *
     * @return TCDaySlot
     */
    public function setOthours($othours)
    {
        $this->othours = $othours;

        return $this;
    }

    /**
     * Get othours
     *
     * @return string
     */
    public function getOthours()
    {
        return $this->othours;
    }

    /**
     * Set holidayhours
     *
     * @param string $holidayhours
     *
     * @return TCDaySlot
     */
    public function setHolidayhours($holidayhours)
    {
        $this->holidayhours = $holidayhours;

        return $this;
    }

    /**
     * Get holidayhours
     *
     * @return string
     */
    public function getHolidayhours()
    {
        return $this->holidayhours;
    }

    /**
     * Set type1hours
     *
     * @param string $type1hours
     *
     * @return TCDaySlot
     */
    public function setType1hours($type1hours)
    {
        $this->type1hours = $type1hours;

        return $this;
    }

    /**
     * Get type1hours
     *
     * @return string
     */
    public function getType1hours()
    {
        return $this->type1hours;
    }

    /**
     * Set type2hours
     *
     * @param string $type2hours
     *
     * @return TCDaySlot
     */
    public function setType2hours($type2hours)
    {
        $this->type2hours = $type2hours;

        return $this;
    }

    /**
     * Get type2hours
     *
     * @return string
     */
    public function getType2hours()
    {
        return $this->type2hours;
    }

/* Again I removed a few getter/setters to minimize size */

    /**
     * Set note
     *
     * @param string $note
     *
     * @return TCDaySlot
     */
    public function setNote($note)
    {
        $this->note = $note;

        return $this;
    }

    /**
     * Get note
     *
     * @return string
     */
    public function getNote()
    {
        return $this->note;
    }

    /**
     * Set employee
     *
     * @param \CockpitBundle\Entity\Employee $employee
     *
     * @return TCDaySlot
     */
    public function setEmployee(\CockpitBundle\Entity\Employee $employee = null)
    {
        $this->employee = $employee;

        return $this;
    }

    /**
     * Get employee
     *
     * @return \CockpitBundle\Entity\Employee
     */
    public function getEmployee()
    {
        return $this->employee;
    }
}

这是DaySlot实体(TCDaySlot)

/**
 * Creates a new tCTimeCard entity.
 *
 * @Route("/new", name="tctimecard_new")
 * @Method({"GET", "POST"})
 */
public function newAction(Request $request)
{
    $tcTimeCard = new TCTimeCard();
    $em = $this->getDoctrine()->getManager();
    $periodStart = $em->getRepository('CockpitBundle:TCTimeCard')->getBestDate();
    $username = $this->getUser()->getUsername();
    $employee = $em->getRepository('CockpitBundle:Employee')->findOneByUsername($username);
    $status = $em->getRepository('CockpitBundle:TCStatus')->findOneById(1);
    $tcTimeCard->setPeriodBegin($periodStart);
    $tcTimeCard->setStatus($status);
    $tcTimeCard->setEmployee($employee);

    $form = $this->createForm('CockpitBundle\Form\TCTimeCardType', $tcTimeCard);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $cnt=0;
        foreach($form->get('tcdayslots')  as $slotform) {
            print "Cnt =". ++$cnt . "<BR>";
        }
        $em->persist($tcTimeCard);
        $em->flush();
        die("done");
        return $this->redirectToRoute('tctimecard_show', array('id' => $tcTimeCard->getId()));
    }
    $days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
    $headings = ['-2' => 'Regular','-1' => "Overtime", '0' => "Holiday"];
    foreach ($em->getRepository('CockpitBundle:TimeOffType')->findAll() as $tot) {
        $headings[$tot->getId()] = $tot->getType();
    }
    $headings['99'] = "Comments";
    $date = $periodStart;
    for ($i = 1; $i<=2; $i++) {
        foreach ($days as $day) {
            $tcDaySlot = new \CockpitBundle\Entity\TCDaySlot;
            $tcDaySlot->setDate($date->format('Y-m-d'));
            //$tcsForm = $this->slotForm($tcDaySlot,$date,$employee)->createView();
            //$tcsForms[$date->format('m/d/y')] = $tcsForm;
            $dates[$date->format('m/d/y')] = $day;
            $date->add(new \DateInterval('P1D'));
            $tcTimeCard->addTcdayslot($tcDaySlot);
        }
    }
    $cnt2 =0;
    foreach ($tcTimeCard->getTcdayslots() as $dayslot) {
        print "Cnt2 = " . ++$cnt2 . " " . $dayslot->getDate() . "<BR>";
    }
    dump($form->getData());
    return $this->render('tctimecard/new.html.twig', array(
        'tCTimeCard' => $tcTimeCard,
        'form' => $form->createView(),
        'headings' => $headings,
        'emp' => $employee,
        'dates' => $dates,
    ));
}

这是控制器TCTimeCardController

{% extends 'base.html.twig' %}

{% block body %}
    <div class="status">
        <h1>Time Card for {{ emp.displayname }} Period Beginning: {{ dates|keys|first }}</h1>
    </div>

        keys = {{form|keys|join('|') }}<BR>
    {{ form_start(form) }}
    {{ form_widget(form) }}

    <table class="timecard">
        <tr>
            <th>Date</th>
        {% for id,heading in headings %}
                <th class="tcheader">
                    {{ heading}}
                </td>
        {% endfor %}
        </tr>
        Length = {{ form.tcdayslots|length }}
        {% for tcdayslot in form.tcdayslots %}
            <tr>
                {% if day | slice(0,1) == 'S' %}
                    <td class="weekend">{{ date }} {{ day }}</td>
                {% else %}
                    <td>{{ date }} {{ day }}{{ form_start(tcdayslot) }}</td>
                {% endif %}
                <td>
                    {{ form_row(tcdayslot.reghours) }}
                </td>
                <td>
                    {{ form_row(tcdayslot.othours) }}
                </td>
                <td>
                    {{ form_widget(tcdayslot.holidayhours) }}
                </td>
                <td>
                    {{ form_widget(tcdayslot.type1hours) }}
                </td>
                <td>
                    {{ form_widget(tcdayslot.type2hours) }}
                </td>
                <td>
                    {{ form_widget(tcdayslot.type3hours) }}
                </td>
                <td>
                    {{ form_widget(tcdayslot.type4hours) }}
                </td>
                <td>
                    {{ form_widget(tcdayslot.type5hours) }}
                </td>
                <td>
                    {{ form_widget(tcdayslot.note) }}
                    {{ form_end(tcdayslots) }}
                </td>
            </tr>
        {% endfor %}
        <tr><td>TOTALS</td>
        </table>
        <input type="submit" value="Create" />
    {{ form_end(form) }}

    <ul>
        <li>
            <a href="{{ path('tctimecard_index') }}">Back to the list</a>
        </li>
    </ul>
{% endblock %}

最后,这是我用来显示表格的树枝

Cells

1 个答案:

答案 0 :(得分:0)

我终于弄清楚发生了什么。 foreach $ days循环中的以下行:

$tcDaySlot = new \CockpitBundle\Entity\TCDaySlot;

不会调用构造函数。我改成了:

$tcDaySlot = TCDaySlot();

并为实体添加了一个use语句,似乎已正确连接了该集合。我认为它是一个静态类实例化而不是对象的实例,或者我只是错过了parens?

我还必须删除twig中每个集合对象的form_start和form_end,因为只创建了一个表单(呃,你不能在HTML中嵌入表单)。