一意の変数を持つエンティティを作成しました。ORM を一意に設定し、バリデーターにも設定しました。これにより、一意の名前を持つ新しい行を作成できますが、更新するように指定しただけでも、既存の行を更新することはできません。
検証.yml:
# src/Battlemamono/DatabaseBundle/Resources/config/validation.yml
Battlemamono\DatabaseBundle\Entity\Mamono:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: name
message: A mamono with this name already exists.
properties:
id:
- Type:
type: integer
name:
- NotBlank: ~
- MaxLength: 30
- Type:
type: string
family1:
- NotBlank: ~
- MaxLength: 30
- Type:
type: string
- Choice : { callback: getFamily }
family2:
- MaxLength: 30
- Type:
type: string
- Choice : { callback: getFamily }
element1:
- NotBlank: ~
- MaxLength: 30
- Type:
type: string
- Choice : { callback: getElements }
element2:
- MaxLength: 30
- Type:
type: string
- Choice : { callback: getElements }
disposition:
- NotBlank: ~
- MaxLength: 100
- Type:
type: string
diet:
- NotBlank: ~
- MaxLength: 100
- Type:
type: string
previousForm:
- MaxLength: 30
- Type:
type: string
nextForm:
- MaxLength: 30
- Type:
type: string
evolution:
- MaxLength: 30
- Type:
type: string
evolutionLove:
- Type:
type: bool
tags:
- Type:
type: string
- MaxLength: 100
description:
- Type:
type: string
- NotBlank: ~
これを行うアクション:
public function editProccessAction($id, Request $request)
{
$form = $this->createForm(new MamonoType());
if ($request->isMethod('POST'))
{
$form->bind($request);
if ($form->isValid())
{
$FormData = $form->getData();
$em = $this->getDoctrine()->getManager();
$mamono = $em->getRepository('BattlemamonoDatabaseBundle:Mamono')->find($id);
if (!$mamono) {
$this->get('session')->getFlashBag()->add('notice', 'There is no such mamono in the database. Create it instead!');
return $this->redirect($this->generateUrl('battlemamono_database_create'));
}
$mamono->setName($FormData->getName());
$mamono->setFamily1($FormData->getFamily1());
$mamono->setFamily2($FormData->getFamily2());
$mamono->setElement1($FormData->getElement1());
$mamono->setElement2($FormData->getElement2());
$mamono->setDisposition($FormData->getDisposition());
$mamono->setDiet($FormData->getDiet());
$mamono->setPreviousForm($FormData->getPreviousForm());
$mamono->setNextForm($FormData->getNextForm());
$mamono->setEvolution($FormData->getEvolution());
$mamono->setEvolutionLove($FormData->getEvolutionLove());
$mamono->setTags($FormData->getTags());
$mamono->setDescription($FormData->getDescription());
$mamono->setUpdatedBy();
$em->flush();
$this->get('session')->getFlashBag()->add('notice', 'the Mamono was updated.');
return $this->redirect($this->generateUrl('battlemamono_database_homepage'));
}
else
{
return new Response($form->getErrorsAsString());
}
}
}