简体   繁体   English

有没有更好的方法来处理yii2凝块形式的唯一字段?

[英]Is there a better way to deal with unique field in yii2 curd form?

In my yii2 form I have a field name number , which is unique in DB. 在yii2表单中,我有一个字段名称number ,在DB中是唯一的。

This number will be used for sorting in frontend. number将用于前端排序。

Whats the best way to deal with this field in backend? 在后端处理此字段的最佳方法是什么?

I guess you're looking how to create a primary key ? 我猜您正在寻找如何创建主键 primary key must be unique here I declare number_field as primary key which is unique. 主键必须是唯一的,这里我将number_field声明为唯一的主键。

Here is an example of a table creation: 这是表创建的示例:

CREATE TABLE `table_name` (
 `number_field` bigint(20) NOT NULL,
 `another_field` bigint(20) DEFAULT NULL,
 `extra_field` varchar(255) DEFAULT NULL,
 PRIMARY KEY (`number_field`),
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

also you can use UNIQUE KEY prop instead of PRIMARY KEY 也可以使用UNIQUE KEY prop代替PRIMARY KEY

you can change existing table like this 您可以像这样更改现有表

alter table 
your_table_name 
add unique (number_field)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM