symfony2唯一验证器无法更新行

时间:2013-02-02 16:17:22

标签: php symfony doctrine-orm yaml

我创建了一个具有唯一变量的实体。我将ORM设置为唯一且在验证器中也是如此。这允许我创建具有唯一名称的新行,但不允许我更新现有行,即使我只是指定更新它。

validation.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());
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您实际上并没有保存您的实体:

//...
$mamono->save();
$em->flush();