ID的Doctrine2实体不要将ID插入数据库

ID的Doctrine2实体不要将ID插入数据库

本文介绍了具有varchar ID的Doctrine2实体不要将ID插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试在ZF2应用程序中为Doctrine2创建实体。
我的实体应具有id varchar(15),但是当我尝试创建新行时,doctrine2并未将此ID推送到数据库中。

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...

是否需要更改为

推荐答案

Doctrine处理 @ ORM\每个数据库的GeneratedValue(strategy = Identity)不同。如果是MySql,它将插入一个生成的值(也称为 AUTO_INCREMENT ),从而生成 integer 作为ID。请查看。

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.

在您的情况下,您不应该使用 GeneratedValue 注释及其策略。因此,将其完全删除。如果需要表单的ID,则可以在实体中设置一个构造函数,该构造函数将为该实体设置ID。

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...
}

注意提及 GeneratedValue 注释与 GenerateValue(strategy = None)相同。

当然,您可以在创建ID之后再设置ID,但是此实体的每个实例都需要一个ID,因此最好由构造函数强制设置它。

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']);
}

如前所述,您想通过表单输入来设置 ID 但是,我要指出的是,用户可以放入重复的主键。为了捕获它,您必须检查对象是否已经存在,在这种情况下,我建议使用 \DoctrineModule\Validator ObjectNoObjectExists 。请参见关于学说验证器。

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.

因此,要检查是否存在带有ID的对象,可以将此验证器添加到表单的输入过滤器中:

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'
                    )
                )
            )
        )
    );
}

现在我们知道提供的 ID 可用于新的 Checkpoints 实体。

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

这篇关于具有varchar ID的Doctrine2实体不要将ID插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:44