有没有办法使用doctrine2 强制执行唯一列?

发布于 2024-12-03 02:30:43 字数 1060 浏览 3 评论 0原文

我知道我总是可以使用 MYSQL 模式设置唯一的数据库键,但是我只是好奇 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;
}

I know I can always set a unique DB key using MYSQL schema however was just curious if ORM's like doctrine allowed you to set a column to be unique in code?

For example how can I make it in code so that username's are unique in code at run time?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清引 2024-12-10 02:30:43

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

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

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 年可能还没有这个功能。

Just providing an alternate solution that is a little simpler.

If it is a single column, you could simply add a unique on the column definition:

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

Documentation on this:
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#annref_column

If you need a unique index on multiple columns, you still need to use the method Andreas provided.

note: I'm not sure since which version this is available. It might be this was not yet available in 2011.

仄言 2024-12-10 02:30:43

我假设这就是你想要的?

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

由于我没有你的代码,我无法给你一个实用的 例子。

I'm assuming this is what you want?

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

Since I don't have your code I can't give you a practical example.

安静被遗忘 2024-12-10 02:30:43

您必须在 @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

You have to set the uniq constraints in the @Table declaration

@UniqueConstraint

Annotation is used inside the @Table annotation on the entity-class
level. It allows to hint the SchemaTool to generate a database unique
constraint on the specified table columns. It only has meaning in the
SchemaTool schema generation context.

Required attributes:
name: Name of the Index,
columns: Array of columns.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文