symfony2 学习2 表单创建 使用

第一步 使用 命令 创建 实体类

php bin/console doctrine:generate:entity

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * ApiType
 *
 * @ORM\Table(name="api_type")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ApiTypeRepository")
 */
class ApiType
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @var int
     *
     * @ORM\Column(name="status", type="integer", nullable=true)
     */
    private $status;

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

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


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

    /**
     * Set title
     *
     * @param string $title
     *
     * @return ApiType
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

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

    /**
     * Set status
     *
     * @param integer $status
     *
     * @return ApiType
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * Get status
     *
     * @return int
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     *
     * @return ApiType
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

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

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     *
     * @return ApiType
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

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


第二部 创建对应的控制器

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use AppBundle\Entity\ApiType;
use AppBundle\Form\ApiTypeType;

/**
 * @Route("/apitype");
 */
class ApiTypeController extends Controller
{
    /**
     * @Route("/index",name="apitype_index")
     */
    public function indexAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
 
        $qb = $em->getRepository('AppBundle:ApiType')->createQueryBuilder('n');
     
        $paginator = $this->get('knp_paginator');
        $pagination = $paginator->paginate($qb, $request->query->getInt('page', 1),3);

        return $this->render('AppBundle:ApiType:index.html.twig', array(
            'pagination' => $pagination,
        ));
    }

    /**
     * @Route("/store")
     */
    public function storeAction(Request $request)
    {
        $apitype = new ApiType();

        $form = $this->createForm(ApiTypeType::class, $apitype);

        if ($request->getMethod()=='POST')
        {

            $form->handleRequest($request);

            if($form->isValid()){

                $em=$this->getDoctrine()->getManager();
                $em->persist($apitype);
                $em->flush();

                return $this->redirect($this->generateUrl('apitype_index'), 301);
            }
        }
        return $this->render('AppBundle:ApiType:store.html.twig', array(
            // ...
            'form' => $form->createView(),
        ));
    }

    /**
     * @Route("/delete")
     */
    public function deleteAction()
    {
        return $this->render('AppBundle:ApiType:delete.html.twig', array(
            // ...
        ));
    }

}


第三部 创建对应的form

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;


class ApiTypeType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('status')
            ->add('updated_at', DateTimeType::class)
            ->add('created_at', DateTimeType::class)
            ->add('save', SubmitType::class)
        ;
    }
    
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\ApiType'
        ));
    }
}



第四部 创建对应的view


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


{% block body %}
<div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-12">
         <h1></h1>
            <ol class="breadcrumb">
                <li>
                    <a href=""> 用户管理</a>
                </li>
                <li>
                    <a href="">添加用户</a>
                </li>

            </ol>
        </div>
    </div>
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>

                        </div>
                    </div>
                    <div class="ibox-content">
                      
                       
                          
                          {{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endblock %}



你可能感兴趣的:(symfony2 学习2 表单创建 使用)