Last active
May 17, 2016 09:16
-
-
Save LeoOnTheEarth/a8b5e22a46bf59db4b20765deb5bcf30 to your computer and use it in GitHub Desktop.
Symfony Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# src/AppBundle/Resources/views/propel/edit.html.twig #} | |
{% extends '::base.html.twig' %} | |
{% block body %} | |
{{ form_start(form) }} | |
{{ form_widget(form) }} | |
<button>submit</button> | |
{{ form_end(form) }} | |
{% endblock body %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/AppBundle/Controller/PrescriptionController.php | |
namespace AppBundle\Controller; | |
use AppBundle\Propel\Prescription; | |
use AppBundle\Propel\PrescriptionQuery; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
use AppBundle\Form\Type\PrescriptionType; | |
class PrescriptionController extends Controller | |
{ | |
/** | |
* @Route("/edit/{id}", name="edit", defaults={"id" = 0}) | |
*/ | |
public function editAction(Request $request, $id = 0) | |
{ | |
$queryFactory = $this->get('kazz.propel.query_factory'); | |
$prescription = $queryFactory->get(PrescriptionQuery::class)->findOneById($id); | |
if (empty($prescription)) { | |
$prescription = new Prescription(); | |
} | |
$form = $this->createForm(PrescriptionType::class, $prescription); | |
if ($request->isMethod('POST')) { | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
$prescription->save(); | |
$editUrl = $this->generateUrl( | |
'edit', | |
[ | |
'id' => $prescription->getId(), | |
] | |
); | |
return $this->redirect($editUrl); | |
} | |
} | |
// replace this example code with whatever you need | |
return $this->render('AppBundle:propel:edit.html.twig', [ | |
'form' => $form->createView(), | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/AppBundle/Form/Type/PrescriptionType.php | |
namespace AppBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class PrescriptionType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->add('see_dr_date'); | |
$builder->add('period'); | |
$builder->add('times'); | |
} | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'AppBundle\Propel\Prescription', | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$prescriptionQuery = $this->get('kazz.propel.query_factory')->get(PrescriptionQuery::class); | |
$prescription = $prescriptionQuery->findOneById(3); | |
if (empty($prescription)) | |
{ | |
throw new \Exception('rx id not found'); | |
} | |
$schedule1 = new Schedule(); | |
$schedule1->setDate(new \DateTime('2016-01-06')); | |
$schedule1->setSorted(1); | |
$schedule2 = new Schedule(); | |
$schedule2->setDate(new \DateTime('2016-02-05')); | |
$collection = new Collection([$schedule1, $schedule2]); | |
$prescription->setSchedules($collection); | |
$prescription->addSchedule($schedule1); | |
$prescription->addSchedule($schedule2); | |
$prescription->save(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/AppBundle/Form/Type/ScheduleType.php | |
namespace AppBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class ScheduleType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->add('date'); | |
$builder->add('sorted'); | |
} | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'AppBundle\Propel\Prescription', | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- src/AppBundle/Resources/config/propel/schema.xml --> | |
<?xml version="1.0" encoding="UTF-8"?> | |
<database name="default" defaultIdMethod="native" namespace="AppBundle\Propel"> | |
<table name="prescriptions" phpName="Prescription"> | |
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/> | |
<column name="see_dr_date" type="date" required="true"/> | |
<column name="period" type="integer" required="true"/> | |
<column name="times" type="integer" required="true"/> | |
<behavior name="timestampable"/> | |
</table> | |
<table name="schedules" phpName="Schedule"> | |
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/> | |
<column name="rx_id" type="integer" required="true" /> | |
<column name="date" type="date" required="true"/> | |
<column name="sorted" type="tinyint" size="1" defaultValue="0" /> | |
<foreign-key foreignTable="prescriptions" phpName="Prescription" skipSql="true"> | |
<reference local="rx_id" foreign="id"/> | |
</foreign-key> | |
<behavior name="timestampable"/> | |
</table> | |
</database> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment