有没有办法使用doctrine2 强制执行唯一列?
我知道我总是可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是提供一个更简单的替代解决方案。
如果它是单个列,您可以简单地在列定义上添加唯一的:
有关此的文档:
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:
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.
我假设这就是你想要的?
http://www.doctrine -project.org/docs/orm/2.0/en/reference/annotations-reference.html#annref-uniqueconstraint
由于我没有你的代码,我无法给你一个实用的 例子。
I'm assuming this is what you want?
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.
您必须在 @Table 声明中设置 uniq 约束
来源: 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
source: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/annotations-reference.html#annref-uniqueconstraint