Fix some id generation issues

This commit is contained in:
Timothy Warren 2018-01-04 10:42:36 -05:00
parent 9c6c44f140
commit 46d5310ce5
14 changed files with 163 additions and 64 deletions

View File

@ -3,20 +3,21 @@
namespace CameraBundle\Controller;
use CameraBundle\Entity\Lenses;
use CameraBundle\Form\LensesType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\{Method, Route};
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\{Request, RedirectResponse};
/**
* Lense controller.
* Lens controller.
*
* @Route("lens")
*/
class LensesController extends Controller
{
/**
* Lists all lense entities.
* Lists all lens entities.
*
* @Route("/", name="lens_index")
* @Method("GET")
@ -40,33 +41,33 @@ class LensesController extends Controller
}
/**
* Creates a new lense entity.
* Creates a new lens entity.
*
* @Route("/new", name="lens_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$lense = new Lenses();
$form = $this->createForm('CameraBundle\Form\LensesType', $lense);
$lens = new Lenses();
$form = $this->createForm(LensesType::class, $lens);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($lense);
$em->persist($lens);
$em->flush();
return $this->redirectToRoute('lens_show', array('id' => $lense->getId()));
return $this->redirectToRoute('lens_show', array('id' => $lens->getId()));
}
return $this->render('lenses/new.html.twig', array(
'lense' => $lense,
'lense' => $lens,
'form' => $form->createView(),
));
}
/**
* Finds and displays a lense entity.
* Finds and displays a lens entity.
*
* @Route("/{id}", name="lens_show")
* @Method("GET")
@ -82,44 +83,66 @@ class LensesController extends Controller
}
/**
* Displays a form to edit an existing lense entity.
* Displays a form to edit an existing lens entity.
*
* @Route("/{id}/edit", name="lens_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, Lenses $lense)
public function editAction(Request $request, Lenses $lens)
{
$deleteForm = $this->createDeleteForm($lense);
$editForm = $this->createForm('CameraBundle\Form\LensesType', $lense);
$deleteForm = $this->createDeleteForm($lens);
$deacquireForm = $this->createDeacquireForm($lens);
$editForm = $this->createForm(LensesType::class, $lens);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('lens_edit', array('id' => $lense->getId()));
return $this->redirectToRoute('lens_edit', array('id' => $lens->getId()));
}
return $this->render('lenses/edit.html.twig', array(
'lense' => $lense,
'lense' => $lens,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
'deacquire_form' => $deacquireForm->createView()
));
}
/**
* Deletes a lense entity.
* Moves a camera to the previouslyOwned table
*
* @Route("/{id}/deacquire", name="lens_deacquire")
* @Method("POST")
* @param Request $request
* @param Lenses $lens
* @return RedirectResponse
*/
public function deacquireAction(Request $request, Lenses $lens)
{
$form = $this->createDeacquireForm($lens);
$form->handleRequest($request);
$repository = $this->getDoctrine()->getRepository(Lenses::class);
$repository->deacquire($lens);
return $this->redirectToRoute('lens_index');
}
/**
* Deletes a lens entity.
*
* @Route("/{id}", name="lens_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, Lenses $lense)
public function deleteAction(Request $request, Lenses $lens)
{
$form = $this->createDeleteForm($lense);
$form = $this->createDeleteForm($lens);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($lense);
$em->remove($lens);
$em->flush();
}
@ -127,17 +150,33 @@ class LensesController extends Controller
}
/**
* Creates a form to delete a lense entity.
* Creates a form to delete a lens entity.
*
* @param Lenses $lense The lense entity
* @param Lenses $lens The lens entity
*
* @return \Symfony\Component\Form\Form The form
* @return FormInterface The form
*/
private function createDeleteForm(Lenses $lense)
private function createDeleteForm(Lenses $lens): FormInterface
{
return $this->createFormBuilder()
->setAction($this->generateUrl('lens_delete', array('id' => $lense->getId())))
->setAction($this->generateUrl('lens_delete', array('id' => $lens->getId())))
->setMethod('DELETE')
->getForm();
}
/**
* Creates a form to move
*
* @param Lenses $lens The lens entity
*
* @return FormInterface
*/
private function createDeacquireForm(Lenses $lens): FormInterface
{
return $this->createFormBuilder()
->setAction($this->generateUrl('lens_deacquire', ['id' => $lens->getId()]))
->setMethod('POST')
->getForm();
}
}

View File

@ -3,6 +3,7 @@
namespace CameraBundle\Controller;
use CameraBundle\Entity\PreviouslyOwnedCamera;
use CameraBundle\Form\PreviouslyOwnedCameraType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
@ -20,6 +21,7 @@ class PreviouslyOwnedCameraController extends Controller
*
* @Route("/", name="previously-owned-camera_index")
* @Method("GET")
* @throws \UnexpectedValueException
*/
public function indexAction()
{
@ -54,10 +56,11 @@ class PreviouslyOwnedCameraController extends Controller
*
* @Route("/{id}/edit", name="previously-owned-camera_edit")
* @Method({"GET", "POST"})
* @throws \LogicException
*/
public function editAction(Request $request, PreviouslyOwnedCamera $previouslyOwnedCamera)
{
$editForm = $this->createForm('CameraBundle\Form\PreviouslyOwnedCameraType', $previouslyOwnedCamera);
$editForm = $this->createForm(PreviouslyOwnedCameraType::class, $previouslyOwnedCamera);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {

View File

@ -17,7 +17,7 @@ class BatteryType
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
}

View File

@ -21,7 +21,8 @@ class Camera
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\SequenceGenerator(sequenceName="camera_id_seq", allocationSize=1, initialValue=1)
*/
private $id;

View File

@ -17,7 +17,8 @@ class CameraType
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\SequenceGenerator(sequenceName="camera.camera_type_id_seq", allocationSize=1, initialValue=1)
*/
private $id;

View File

@ -19,7 +19,8 @@ class Flash
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\SequenceGenerator(sequenceName="camera.flash_id_seq", allocationSize=1, initialValue=1)
*/
private $id;

View File

@ -8,7 +8,7 @@ use Doctrine\ORM\Mapping as ORM;
* Camera.lenses
*
* @ORM\Table(name="lenses", schema="camera")
* @ORM\Entity
* @ORM\Entity(repositoryClass="CameraBundle\Repository\LensesRepository")
*/
class Lenses
{
@ -19,7 +19,8 @@ class Lenses
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\SequenceGenerator(sequenceName="camera.lenses_id_seq", allocationSize=1, initialValue=1)
*/
private $id;

View File

@ -19,7 +19,7 @@ class PreviouslyOwnedCamera
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
}

View File

@ -19,7 +19,7 @@ class PreviouslyOwnedFlash
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

View File

@ -18,7 +18,7 @@ class PreviouslyOwnedLenses
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;

View File

@ -0,0 +1,41 @@
<?php
namespace CameraBundle\Repository;
use CameraBundle\Entity\{Lenses, PreviouslyOwnedLenses};
use Doctrine\ORM\EntityRepository;
class LensesRepository extends EntityRepository
{
/**
* @param Lenses $currentRecord
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function deacquire(Lenses $currentRecord)
{
$em = $this->getEntityManager();
$currentRecord->setFormerlyOwned(true);
$newRecord = new PreviouslyOwnedLenses();
$old = new \ReflectionObject($currentRecord);
$new = new \ReflectionObject($newRecord);
foreach ($old->getProperties() as $property) {
$propertyName = $property->getName();
if ($new->hasProperty($propertyName)) {
$newProperty = $new->getProperty($propertyName);
$newProperty->setAccessible(true);
$property->setAccessible(true);
$newProperty->setValue($newRecord, $property->getValue($currentRecord));
}
}
// dump($newRecord);
$em->persist($newRecord);
//$em->remove($currentRecord);
$em->flush();
}
}

View File

@ -17,6 +17,7 @@
<th>Actions</th>
<th>Id</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@ -31,7 +32,7 @@
</td>
<td><a href="{{ path('camera-type_show', { 'id': cameraType.id }) }}">{{ cameraType.id }}</a></td>
<td>{{ cameraType.type }}</td>
<td>{{ cameraType.description }}</td>
</tr>
{% endfor %}
</tbody>

View File

@ -1,39 +1,43 @@
{% extends 'form.html.twig' %}
{% block form %}
<h2>Camera Type</h2>
<h2>Camera Type</h2>
<div class="small callout">
<ul>
<li>
<a href="{{ path('camera-type_index') }}">Back to the list</a>
</li>
<li>
<a href="{{ path('camera-type_edit', { 'id': cameraType.id }) }}">Edit</a>
</li>
</ul>
<div class="small callout">
<ul>
<li>
<a href="{{ path('camera-type_index') }}">Back to the list</a>
</li>
<li>
<a href="{{ path('camera-type_edit', { 'id': cameraType.id }) }}">Edit</a>
</li>
</ul>
<hr />
<hr/>
{{ form_start(delete_form) }}
<button type="submit" class="alert button">Delete Camera Type</button>
{{ form_end(delete_form) }}
{{ form_start(delete_form) }}
<button type="submit" class="alert button">Delete Camera Type</button>
{{ form_end(delete_form) }}
</div>
<div class="large primary callout">
<table>
<tbody>
<tr>
<th>Id</th>
<td>{{ cameraType.id }}</td>
</tr>
<tr>
<th>Type</th>
<td>{{ cameraType.type }}</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<th>Id</th>
<td>{{ cameraType.id }}</td>
</tr>
<tr>
<th>Type</th>
<td>{{ cameraType.type }}</td>
</tr>
<tr>
<th>Description</th>
<td>{{ cameraType.description }}</td>
</tr>
</tbody>
</table>
</div>
{% endblock %}

View File

@ -19,6 +19,13 @@
<hr />
{{ form_start(deacquire_form) }}
{{ form_widget(deacquire_form) }}
<button type="submit" class="button">De-acquire</button>
{{ form_end(deacquire_form) }}
<hr />
{{ form_start(delete_form) }}
<button type="submit" class="alert button">Delete Lens</button>
{{ form_end(delete_form) }}