1

これを簡単なウェブサイトに実装して、お金を節約しようとしています。そのため、ページを更新せずにデータを保存できるようにしたいと考えています。

コントローラ:

namespace yz\BstBundle\Controller;

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

use yz\BstBundle\Entity\Money;
use yz\BstBundle\Form\MoneyType;

/**
 * Money controller.
 *
 */
class MoneyController extends Controller
{
    /**
     * Lists all Money entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('yzBstBundle:Money')->findAll();

        return $this->render('yzBstBundle:Money:index.html.twig', array(
            'entities' => $entities,
        ));
    }

    /**
     * Creates a new Money entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity  = new Money();
        $form = $this->createForm(new MoneyType(), $entity);
        $form->bind($request);

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

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

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

    /**
     * Displays a form to create a new Money entity.
     *
     */
    public function newAction()
    {
        $entity = new Money();
        $form   = $this->createForm(new MoneyType(), $entity);

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

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

        $entity = $em->getRepository('yzBstBundle:Money')->find($id);

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

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

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

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

        $entity = $em->getRepository('yzBstBundle:Money')->find($id);

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

        $editForm = $this->createForm(new MoneyType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

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

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

        $entity = $em->getRepository('yzBstBundle:Money')->find($id);

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

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createForm(new MoneyType(), $entity);
        $editForm->bind($request);

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

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

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

    /**
     * Deletes a Money entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->bind($request);

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

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

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

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

    /**
     * Creates a form to delete a Money entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}

小枝テンプレート:

{% extends '::base.html.twig' %}

    {% block body -%}
        <h1>Money creation</h1>

        <form action="{{ path('money_create') }}" method="post" {{ form_enctype(form) }}>
            {{ form_widget(form) }}
            <p>
                <button type="submit">Create</button>
            </p>
        </form>

            <ul class="record_actions">
        <li>
            <a href="{{ path('money') }}">
                Back to the list
            </a>
        </li>
    </ul>
    {% endblock %}

フォームタイプ

namespace yz\BstBundle\Form;

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

class MoneyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('created_at')
            ->add('type')
            ->add('price')
            ->add('comment')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'yz\BstBundle\Entity\Money'
        ));
    }

    public function getName()
    {
        return 'yz_bstbundle_moneytype';
    }
}
4

2 に答える 2

4

変更された質問に答えるために、ここにあなたのケースの解決策があります:

コントローラ:

/**
 * Creates a new Money entity.
 *
 */
public function createAction(Request $request)
{
    $entity  = new Money();
    $form = $this->createForm(new MoneyType(), $entity);
    $form->bind($request);

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

        if($request->isXmlHttpRequest()) {
             $response = new Response();
             $output = array('success' => true, 'type' => $entity->getType(), 'id' => $entity->getId(), 'comment' => $entity->getComment(), 'price' => $entity->getPrice());
             $response->headers->set('Content-Type', 'application/json');
             $response->setContent(json_encode($output));

             return $response;
        }

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

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

テンプレート

{% extends '::base.html.twig' %}

{% block body %}
    <h1>Money creation</h1>

    <form action="{{ path('money_create') }}" method="post" {{ form_enctype(form) }}>
        {{ form_widget(form) }}
        <p>
            <button type="submit">Create</button>
        </p>
    </form>

        <ul class="record_actions">
    <li>
        <a href="{{ path('money') }}">
            Back to the list
        </a>
    </li>
</ul>
<div id="result"></div>
{% endblock %}

{% block javascripts %}
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script type="text/javascript">
     $().ready(function() {
         $("form").submit(function(e) {
             e.preventDefault();
             var $url = $(this).attr('action');
             var $data = $(this).serialize();
             $.ajax({
                type: "POST",
                url: $url,
                data: $data
             }).done(function( result ) {
                if(result.success) {
                    $('#result').html('<span>Monetary expense correctly saved!<br/> The data are:<br/>id: '+ result.id +'<br/>type: '+ result.type +'<br/>price: '+ result.price +'<br/>comment: '+ result.comment +'</span>');
                }
             });
         });
     });
     </script>
{% endblock %}

このソリューションは完全に機能しており、クラス JsonResponse() やオブジェクトを JMS シリアライザー バンドルとしてシリアル化するのに適した優れたバンドルの使用など、高速化するための多くの代替方法を時間とともに学習します。

しかし、火に肉を入れすぎないでください。

お役に立てば幸いです!

編集:

$entity-getPrice()の代わりにタイプミスしました$entity->getPrice()

ajax 呼び出しのタイプは POST でなければなりません。

まだ入力していない場合は、コントローラーの先頭に次の行を忘れずに入力してください。

use Symfony\Component\HttpFoundation\Response;

今では完全に機能します。

于 2013-07-31T00:30:55.373 に答える