Doctrine2 entity with varchar id don't insert id into database

霸气de小男生 提交于 2019-12-23 20:15:25

问题


I'm trying to make entity for Doctrine2 in ZF2 Application. My entity should have id varchar(15) but when I'm trying to create new row, doctrine2 is not pushing this ID into the database.

In entity generated class I have this:

 /**
 * Checkpoints
 *
 * @ORM\Table(name="checkpoints", uniqueConstraints{@ORM\UniqueConstraint(name="sort", columns={"sort"})}, indexes={@ORM\Index(name="country", columns={"country"})})
 * @ORM\Entity
 */
  class Checkpoints
  {
     /**
     * @var string
     *
     * @ORM\Column(name="id", type="string", length=15, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
     private $id;
  // rest of the entity...

Is there something I need to change to pass this ID from form into the database in doctrine flush?


回答1:


Doctrine handles the @ORM\GeneratedValue(strategy="Identity") differntly for each database. In case of MySql it is will insert a generated value also known as AUTO_INCREMENT resulting in integer as ID. Please take a look at http://docs.doctrine-project.org/en/2.0.x/reference/basic-mapping.html#identifiers-primary-keys.

In your case you shouldn't be using the GeneratedValue annotation including it's strategy. So completely remove it. If you want the ID of your form you could setup a constructor in your entity which will set your ID for that entity.

class Checkpoints {

   public function __constructor($id)
   {
       $this->id = $id;
   }

   /**
     * @var string
     *
     * @ORM\Column(name="id", type="string", length=15, nullable=false)
     * @ORM\Id
     */
     private $id;
  // rest of the entity...
}

Notice that not mentioning the GeneratedValue annotation is the same as GenerateValue(strategy="None").

Ofcourse you can set your ID later after creating it, but each instance of this entity needs an ID so it's better to force it to set it by your constructor.

So in your controller you will probaly use something like this

if ($form->isValid() {
    $formData = $form->getData();
    $checkpoint = new Checkpoints($formData['id']);
}

As you mentioned you would like to set the ID by the input of your form. But let me point out that the user can put in duplicates primary keys. In order to catch it you have to check if an object already exists and in that case I would recommend use \DoctrineModule\Validator\NoObjectExists. See https://github.com/doctrine/DoctrineModule/blob/master/docs/validator.md about Doctrine validators.

So in order to check if an object exists with the ID you can add this validator to your inputfilter of your form:

public function getInputFilterSpecification()
{
    $entityManager = $this->serviceManager->get('Doctrine\ORM\EntityManager');

    return array(
        'id' => array(
            'validators' => array(
                array(
                    'name' => 'DoctrineModule\Validator\NoObjectExists',
                    'options' => array(
                        'object_repository' => $entityManager->getRepository('Application\Entity\Checkpoints'),
                        'fields' => 'id'
                    )
                )
            )
        )
    );
}

So now we know that the provided ID is available for a new Checkpoints entity.




回答2:


Just remove the line

* @ORM\GeneratedValue(strategy="IDENTITY")

form your entity and it will insert the record to your entity



来源:https://stackoverflow.com/questions/24827268/doctrine2-entity-with-varchar-id-dont-insert-id-into-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!