2017-06-07 6 views
0

私は14日間のタイムスロットを持つタイムカードフォームを作成しています。 エンティティ、コントローラ、およびフォームタイプを作成しました。タイムカードのコントローラの新しいアクションでは、エンティティ関数を使用してタイムスロットに14のインスタンスを作成し、タイムカードに追加します。あたかもコレクションが空であるかのように、フォームは空になります。もっと何かしなければならないことはありますか?フォームがエンティティコレクションオブジェクトに正しくバインドされていないようです。私はそれを表示する前にエンティティをダンプしているようだ。ここでSymfony3 - 埋め込みコレクションフォームは、表示されると空です。

はタイムカードエンティティである:ここでは

<?php 

namespace CockpitBundle\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 
/** 
* TCTimeCard 
*/ 
class TCTimeCard 
{ 
    /** 
    * @var integer 
    */ 
    private $id; 

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

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

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

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

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

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

    /** 
    * @var \CockpitBundle\Entity\TCStatus 
    */ 
    private $status; 

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

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

    /** 
    * @var \Doctrine\Common\Collections\Collection 
    */ 
    private $tcslots; 

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

    /** 
    * @var \Doctrine\Common\Collections\Collection 
    */ 
    private $tcdayslots; 


    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->tcslots = new ArrayCollection(); 
     $this->tcdayslots = new ArrayCollection(); 
    } 

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

/* Removed some of the getters/setters to save scrolling */ 

    /** 
    * Set employee 
    * 
    * @param \CockpitBundle\Entity\Employee $employee 
    * 
    * @return TCTimeCard 
    */ 
    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; 
    } 


    /** 
    * @ORM\PreUpdate 
    */ 
    public function updateModifiedDatetime() 
    { 
     // Add your code here 
    } 

    /** 
    * Add tcdayslot 
    * 
    * @param \CockpitBundle\Entity\TCDaySlot $tcdayslot 
    * 
    * @return TCTimeCard 
    */ 
    public function addTcdayslot(\CockpitBundle\Entity\TCDaySlot $tcdayslot) 
    { 
     $this->tcdayslots[] = $tcdayslot; 
     return $this; 
    } 

    /** 
    * Remove tcdayslot 
    * 
    * @param \CockpitBundle\Entity\TCDaySlot $tcdayslot 
    */ 
    public function removeTcdayslot(\CockpitBundle\Entity\TCDaySlot $tcdayslot) 
    { 
     $this->tcdayslots->removeElement($tcdayslot); 
    } 

    /** 
    * Get tcdayslots 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getTcdayslots() 
    { 
     return $this->tcdayslots; 
    } 
} 

DaySlotエンティティ(TCDaySlot)

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

そして、ここではTCTimeCardController

/** 
* 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, 
    )); 
} 

最後に、ここにあるコントローラーです私はフォームを表示するために使用しています。

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

答えて

0

私はついに何が起こっているのか把握しました。 foreach $ daysループの次の行:

$tcDaySlot = new \CockpitBundle\Entity\TCDaySlot; 

コンストラクタは呼び出されません。私はそれを変更:

$tcDaySlot = TCDaySlot(); 

とエンティティの使用ステートメントを追加し、それが適切に収集を接続しているようです。私はそれがオブジェクトのインスタンスの代わりに静的なクラスのインスタンス化だと思うか、私はちょうど括弧を欠いていたのですか?

枝の各コレクションオブジェクトのform_startとform_endを削除する必要がありました。フォームが1つしか作成されていないためです(HTMLでは埋め込みフォームを使用できません)。

関連する問題