これを簡単なウェブサイトに実装して、お金を節約しようとしています。そのため、ページを更新せずにデータを保存できるようにしたいと考えています。
コントローラ:
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';
}
}