cakephp 对于 2 个字段来说是唯一的吗?
我有一个注册表,用户可以在其中填写两个电子邮件地址(email1 和 email2)。营销的要求是它们需要是唯一的(唯一的,就像我们有 10 个用户,那么就会有 10*2=20 个唯一的电子邮件地址)。
该系统已经基于 cakephp 构建,所以我想知道的是,是否有类似于 isUnique 功能(在一个领域唯一)的东西可以开箱即用地执行此操作?或者我注定要自己编写这个代码?提前致谢。
编辑:建立在理查德的例子上,这对我有用:
function checkUnique($data, $fields) {
if (!is_array($fields)) {
$fields = array($fields);
}
foreach($data as $key) {
$checks = $key;
}
if (empty($checks)) {
return true; //allow null
}
foreach($fields as $key) {
$tmp[$key] = $checks;
}
if (isset($this->data[$this->name][$this->primaryKey])) {
$tmp[$this->primaryKey] = "<>".$this->data[$this->name][$this->primaryKey];
}
return $this->isUnique($tmp);
}
I have a registration form in which users can fill in two email address (email1 & email2). Marketing's requirement is that they need to be unique (unique as in if we had 10 users, then there would be 10*2=20 unique email address).
The system is already built on cakephp, so what I'd like to know is, is there something similar to the isUnique feature (unique in one field) that can do this right out of the box? Or am I doomed to code this myself? Thanks in advance.
EDIT: built on Richard's example, this worked for me:
function checkUnique($data, $fields) {
if (!is_array($fields)) {
$fields = array($fields);
}
foreach($data as $key) {
$checks = $key;
}
if (empty($checks)) {
return true; //allow null
}
foreach($fields as $key) {
$tmp[$key] = $checks;
}
if (isset($this->data[$this->name][$this->primaryKey])) {
$tmp[$this->primaryKey] = "<>".$this->data[$this->name][$this->primaryKey];
}
return $this->isUnique($tmp);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在 CakePHP Google Group 上发布了一个解决方案:
http://groups.google.com/group/cake-php/browse_frm/thread/b3a1e4ae3eeb6091/e168f54bac27c163?lnk=gst&q=checkUnique#e168f54bac27c163
将以下内容添加到您的 AppModel :
并在您的模型验证中使用:
I posted a solution to this on the CakePHP Google Group:
http://groups.google.com/group/cake-php/browse_frm/thread/b3a1e4ae3eeb6091/e168f54bac27c163?lnk=gst&q=checkUnique#e168f54bac27c163
Add the following to your AppModel:
and is used in your model validate:
checkUnique
可以编写为isUnique
的包装器。并在您的模型验证中使用:
checkUnique
could just be written as a wrapper forisUnique
.and is used in your model validate:
来自 cakePHP 2.0 文档:
您可以验证一组字段通过提供多个字段并将 $or 设置为 false 来保持唯一:
在跨多个字段制定唯一规则时,请确保在字段列表中包含原始字段。
如果列出的字段未包含在模型数据中,则将其视为空值。您可以考虑根据需要标记列出的字段。
From cakePHP 2.0 documentation:
You can validate that a set of fields are unique by providing multiple fields and set $or to false:
Make sure to include the original field in the list of fields when making a unique rule across multiple fields.
If a listed field isn’t included in the model data, then it’s treated as a null value. You may consider marking the listed fields as required.
是和不是。
是的,您必须自己编写代码,但要在 CakePHP 验证组件中编写。
验证组件具有允许自定义验证规则的机制。本质上,您将函数名称放入 $validate 中(就像通常那样)。你必须定义这个函数;在这种情况下,它非常简单(只需强制执行您的双重 isUnique 要求)。
http://book.cakephp.org/ 2.0/en/models/data-validation.html#custom-validation-rules
Yes and no.
Yes, you will have to code it yourself, but within the CakePHP validation component.
The validation component has a mechanism to allow custom validation rules. Essentially, you put a function name inside $validate (like you normally would). You do have to define the function; in this case, it's pretty simple (just enforce your double isUnique requirement).
http://book.cakephp.org/2.0/en/models/data-validation.html#custom-validation-rules
冒着因提供非 CakePHP 解决方案而遭受重创的风险,让我介绍以下内容。
在数据库中为您需要的任意数量的列创建唯一索引。
标准 SQL 语法是:
将“$this->Model->save()”命令放在“try/catch”块内。在“catch”块中,测试异常的错误代码。在 MySQL 中,唯一键违规是错误代码 23000,但您也应该为其他可能的错误做好准备。
它快速而简单,并且不涉及计算数组括号。
无论如何,您应该始终将数据库访问代码放置在“try/catch”块内。异常处理的一部分应包括记录任何意外的错误消息。您不能指望 CakePHP 为您做一切。
At the risk of being beaten about the head and shoulders for offering a non-CakePHP solution, let me present the following.
Create a unique index in your database over however many columns you need.
Standard SQL syntax for this is:
Place your "$this->Model->save()" command inside of a "try/catch" block. In the "catch" block, test the exception for the error code. In MySQL, a unique key violation is error code 23000, but you should be prepared for other possible errors as well.
It's quick and simple and doesn't involve counting array parentheses.
You should always place database access code inside a "try/catch" block anyway. Part of your exception handling should include logging any unexpected error messages. You can't expect CakePHP to do everything for you.
据我记得,您必须使用模型中的
beforeSave
方法来执行这种强制。我有一个要求,一个对象至少有 N 个 fks 集之一,我只能这样做。编辑:尝试 此线程 看看是否有任何内容可以解决您的问题。
As far as I remember, you have to this kind of enforcement using the
beforeSave
method in the model. I had a requirement that an object have at least one of N fks set, and I could only do it this way.Edit: try this thread to see if anything there solves your problem.