menu

Symfony Cheat sheet

Formulaires

USE


use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

Render


// Create form
$form = $this->createForm(XXXXXType::class, $myEntity);

// If form is send
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
    // Tadatada
}

// For render template
'form' => $form->createView();

Juste un bouton


use Symfony\Component\Form\Extension\Core\Type\SubmitType;

$form = $this->createFormBuilder()
    ->add('submit', SubmitType::class, array('label' => 'Create Task'))
    ->getForm()
;

Route


use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/hello")
 */

Récupérer la session


$session = $this->getRequest()->getSession();
$request->getSession();
$this->getSession();

Repository

Récupérer une entité du jour X


public function getByDate(\Datetime $date)
{
    $from = new \DateTime($date->format("Y-m-d")." 00:00:00");
    $to   = new \DateTime($date->format("Y-m-d")." 23:59:59");

    $qb = $this->createQueryBuilder("e");
    $qb
        ->andWhere('e.date BETWEEN :from AND :to')
        ->setParameter('from', $from )
        ->setParameter('to', $to)
    ;
    $result = $qb->getQuery()->getResult();

    return $result;
}