首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编辑CRUD模板

编辑CRUD模板
EN

Stack Overflow用户
提问于 2014-12-04 19:43:34
回答 1查看 380关注 0票数 1

当我想要显示包含列表的页面时,我发现了另一个显示,与我的模板不同。

我该怎么做呢?你可以在下面找到视图和实体的代码:

代码语言:javascript
复制
{% extends "MyAppProjetBundle::layout.html.twig" %}


{% block content %}


    <table class="record_properties">
        <tbody>
            <tr>
                <th>Id</th>
                <td>{{ entity.id }}</td>
            </tr>
            <tr>
                <th>Titre</th>
                <td>{{ entity.titre }}</td>
            </tr>
            <tr>
                <th>Type</th>
                <td>{{ entity.type }}</td>
            </tr>
            <tr>
                <th>Categorie</th>
                <td>{{ entity.categorie }}</td>
            </tr>
            <tr>
                <th>Ville</th>
                <td>{{ entity.ville }}</td>
            </tr>
            <tr>
                <th>Prix</th>
                <td>{{ entity.prix }}</td>
            </tr>
            <tr>
                <th>Surface</th>
                <td>{{ entity.surface }}</td>
            </tr>
            <tr>
                <th>Description</th>
                <td>{{ entity.description }}</td>
            </tr>
            <tr>
                <th>Validation</th>
                <td>{{ entity.validation }}</td>
            </tr>
            <tr>
                <th>Dateinsertion</th>
                <td>{{ entity.dateInsertion|date('Y-m-d H:i:s') }}</td>
            </tr>
            <tr>
                <th>Iduser</th>
                <td>{{ entity.idUser }}</td>
            </tr>
            <tr>
                <th>Idgerant</th>
                <td>{{ entity.idGerant }}</td>
            </tr>
        </tbody>
    </table>

        <ul class="record_actions">
    <li>
        <a href="{{ path('offre') }}">
            Back to the list
        </a>
    </li>
    <li>
        <a href="{{ path('offre_edit', { 'id': entity.id }) }}">
            Edit
        </a>
    </li>
    <li>{{ form(delete_form) }}</li>
</ul>
{% endblock %}

实体:

代码语言:javascript
复制
<?php

namespace MyApp\ProjetBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use MyApp\ProjetBundle\Entity\Offre;
use MyApp\ProjetBundle\Form\OffreType;

/**
 * Offre controller.
 *
 */
class OffreController extends Controller
{

    /**
     * Lists all Offre entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('MyAppProjetBundle:Offre')->findAll();

        return $this->render('MyAppProjetBundle:Offre:index.html.twig', array(
            'entities' => $entities,
        ));
    }
    /**
     * Creates a new Offre entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity = new Offre();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('offre_show', array('id' => $entity->getId())));
        }

        return $this->render('MyAppProjetBundle:Offre:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Creates a form to create a Offre entity.
     *
     * @param Offre $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createCreateForm(Offre $entity)
    {
        $form = $this->createForm(new OffreType(), $entity, array(
            'action' => $this->generateUrl('offre_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

    /**
     * Displays a form to create a new Offre entity.
     *
     */
    public function newAction()
    {
        $entity = new Offre();
        $form   = $this->createCreateForm($entity);

        return $this->render('MyAppProjetBundle:Offre:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Finds and displays a Offre entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('MyAppProjetBundle:Offre')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Offre entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('MyAppProjetBundle:Offre:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing Offre entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('MyAppProjetBundle:Offre')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Offre entity.');
        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('MyAppProjetBundle:Offre:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
    * Creates a form to edit a Offre entity.
    *
    * @param Offre $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(Offre $entity)
    {
        $form = $this->createForm(new OffreType(), $entity, array(
            'action' => $this->generateUrl('offre_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));

        $form->add('submit', 'submit', array('label' => 'Update'));

        return $form;
    }
    /**
     * Edits an existing Offre entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('MyAppProjetBundle:Offre')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Offre entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createEditForm($entity);
        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $em->flush();

            return $this->redirect($this->generateUrl('offre_edit', array('id' => $id)));
        }

        return $this->render('MyAppProjetBundle:Offre:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    /**
     * Deletes a Offre entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('MyAppProjetBundle:Offre')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Offre entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('offre'));
    }

    /**
     * Creates a form to delete a Offre entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('offre_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }
}

结果:http://i.stack.imgur.com/xbqt9.jpg

EN

回答 1

Stack Overflow用户

发布于 2014-12-04 21:34:04

您可以使用以下内容覆盖CRUD模板:

Symfony docs

如果您想要扩展生成器包(例如,能够在多个项目中共享您的模板),您可以通过在APP_PATH/Resources/SensioGeneratorBundle/skeleton或BUNDLE_PATH/Resources/SensioGeneratorBundle/skeleton中创建相同的目录和文件结构来定义自定义骨架模板。

例如,如果要覆盖CRUD生成器的编辑模板,请在APP_PATH/Resources/SensioGeneratorBundle/skeleton.下创建一个crud/views/edit.html.twig.twig文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27293200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档