Datetime test

Embedding Entities is easy

Assume this entity:

// DateTimeTest.php

<?php

namespace Mopa\Bundle\BootstrapSandboxBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="transmission")
 * @ORM\Entity(repositoryClass="Foo\BarBundle\Entity\TransmissionRepository")
 */
class DateTimeTest
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $name
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var \DateTime $start
     *
     * @ORM\Column(name="start", type="datetime")
     */
    private $start;

    /**
     * @var \DateTime $end
     *
     * @ORM\Column(name="end", type="datetime")
     */
    private $end;

    public function __construct()
    {
        $this->start = new \DateTime('now');
        $this->end = new \DateTime('+1 hour');
    }

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

    /**
     * @param $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * Set name
     *
     * @param  string       $name
     * @return DateTimeTest
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set start
     *
     * @param  \DateTime    $start
     * @return DateTimeTest
     */
    public function setStart($start)
    {
        $this->start = $start;

        return $this;
    }

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

    /**
     * @return \DateTime
     */
    public function getBegin()
    {
        return $this->getStart();
    }

    /**
     * Set end
     *
     * @param  \DateTime    $end
     * @return DateTimeTest
     */
    public function setEnd($end)
    {
        $this->end = $end;

        return $this;
    }

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

FormType Code

// ExampleDateTimeTest.php

<?php
namespace Mopa\Bundle\BootstrapSandboxBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class ExampleDateTimeTest extends AbstractType
{

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('name', null, array(
                    'label' => 'Name Label',
                ))
                ->add('start', null, array(
                    'label' => 'Start Label',
                    'horizontal_input_wrapper_class' => 'col-lg-2'
                ))
                ->add('end', null, array(
                    'label' => 'End Label',
                    'horizontal_input_wrapper_class' => 'col-lg-2'
                ))
        ;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'datetime_test';
    }
}

Template Code

{# MopaBootstrapSandboxBundle:Examples:embeddedType.html.twig #}

{% extends "MopaBootstrapSandboxBundle::layout.html.twig" %}
{% from 'MopaBootstrapBundle::flash.html.twig' import session_flash %}

{% block headline %}Embedded Type Example{% endblock headline %}

{% block content %}
<div class="col-lg-6">
    <form class="form-horizontal" id="your_form_name">
        {{ form_widget(form) }}
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Save changes</button>
            <button type="reset" class="btn">Cancel</button>
        </div>
    </form>
</div>
<div class="col-lg-6">
    <h3>Embedding Entities is easy</h3>
    <p>Assume this entity:</p>

    <pre class="prettyprint">{{ classSource(entity) }}</pre>
</div>
{% endblock content %}

{% set showTemplate = _self %}{% set showForm = formType %}
Fork me on GitHub