【问题标题】:Is there a way to enforce a unique column using doctrine2?有没有办法使用教义2强制执行唯一列?
【发布时间】:2011-09-03 18:43:28
【问题描述】:

我知道我总是可以使用 MYSQL 模式设置唯一的 DB 键,但是我很好奇 ORM 的类似原则是否允许您将列设置为在代码中是唯一的?

例如,我怎样才能在代码中使用户名在运行时在代码中是唯一的?

CREATE TABLE IF NOT EXISTS `user` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `username` varchar(300) COLLATE utf8_unicode_ci NOT NULL,
    `email` varchar(300) COLLATE utf8_unicode_ci NOT NULL,
    `password` varchar(300) COLLATE utf8_unicode_ci NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
<?php

function insert_user($username,$email,$password) 
{
    $user = new User();
    $user->setUsername($username); //HOW CAN I MAKE THIS UNIQUE IN CODE?
    $user->setEmail($email);
    $user->setPassword($password);

    try {
        //save to database
        $this->em->persist($user);
        $this->em->flush();
    }
    catch(Exception $err) {
        die($err->getMessage());
        return false;
    }

    return true;
}

【问题讨论】:

    标签: php doctrine doctrine-orm


    【解决方案1】:

    只是提供一个更简单的替代解决方案。

    如果是单列,您可以简单地在列定义上添加一个唯一的:

    class User
    {
       /**
        * @Column(name="username", length=300, unique=true)
        */
       protected $username;
    }
    

    有关此的文档: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#annref_column

    如果需要多列的唯一索引,还是需要使用 Andreas 提供的方法。

    注意:我不确定这是哪个版本可用。可能是 2011 年还没有。

    【讨论】:

      【解决方案2】:

      我假设这就是你想要的?

      <?php
      /**
       * @Entity
       * @Table(name="ecommerce_products",uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "email"})})
       */
      class ECommerceProduct
      {
      }
      

      http://www.doctrine-project.org/docs/orm/2.0/en/reference/annotations-reference.html#annref-uniqueconstraint

      由于我没有你的代码,我不能给你一个实际的例子。

      【讨论】:

      • 这是您需要更改的表架构,请查看我链接到的文档... uniqueConstraints={@UniqueConstraint(name="myname", columns={"username"}) 可能是你想在你的案例中添加什么。
      • 这是一个更好的答案,因为它适用于更多情况
      • 能否更新链接?这是当前的(2.7)doctrine-project.org/projects/doctrine-orm/en/2.7/reference/…
      【解决方案3】:

      您必须在 @Table 声明中设置 uniq 约束

      @UniqueConstraint

      注解在实体类的@Table注解中使用 等级。它允许提示 SchemaTool 生成唯一的数据库 对指定表列的约束。它只在其中有意义 SchemaTool 架构生成上下文。

      必需属性name:索引的名称, :列数组。

      <?php
      /**
       * @Entity
       * @Table(name="user",uniqueConstraints={@UniqueConstraint(name="username_uniq", columns={"username"})})
       */
      class User
      {
         /**
          * @Column(name="username", length=300)
          */
         protected $username;
      }
      

      来源:http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/annotations-reference.html#annref-uniqueconstraint

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-28
        • 1970-01-01
        • 2018-02-25
        • 2022-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多