如何修改zend Db_RecordExists验证器的where子句? [英] How to modify zend Db_RecordExists validator where clause?
问题描述
我想在where子句中添加多个值的检查. Db_RecordExists只需检查一个已归档.我读过您可以扩展zend validate抽象类,并且可以拥有自己的验证器.请给我一个小例子.
I want to add check for more than one values in where clause. Db_RecordExists just check for one filed. I have read that you can extend zend validate abstract class and you can have your own validator. can I have one little example about this please.
谢谢...
推荐答案
您到底想做什么?对我来说,您甚至不需要自定义验证器.
What are you trying to do exactly? To me, you don't even need a custom validator.
如果仔细阅读Zend_Validate_Db_Abstract
的源代码,您会在构造函数上方注意到此phpDoc:
If you read carefully the source code of Zend_Validate_Db_Abstract
you will notice this phpDoc above the constructor:
提供与Zend_Validate_Db验证程序一起使用的基本配置 设置$ exclude允许从匹配中排除单个记录. 排除可以是包含where子句的字符串,也可以是具有
field
和value
键的数组 定义添加到sql的where子句. 可以选择提供数据库适配器以避免使用已注册的默认适配器.
Provides basic configuration for use with Zend_Validate_Db Validators Setting $exclude allows a single record to be excluded from matching. Exclude can either be a String containing a where clause, or an array with
field
andvalue
keys to define the where clause added to the sql. A database adapter may optionally be supplied to avoid using the registered default adapter.
- 'table'=>要验证的数据库表
- 'schema'=>模式键
- 'field'=>要检查匹配项的字段
- 'exclude'=>从查询中排除的可选where子句或字段/值对
- 'adapter'=>要使用的可选数据库适配器
- 'table' => The database table to validate against
- 'schema' => The schema keys
- 'field' => The field to check for a match
- 'exclude' => An optional where clause or field/value pair to exclude from the query
- 'adapter' => An optional database adapter to use
这意味着,如果要使用多个值检查记录是否存在,则可以简单地通过将where子句传递给验证器(而不是成对的字段/值)来实现:/p>
Which means that if you want to check if a record exists using more than one value, you can do it simply in passing the where clause to the validator instead of a pair field/value:
$where = 'user_id != 110 AND email != "email@example.com"';
$where = array('users', 'email', $where);
$element->addValidator('db_NoRecordExists', true, $where)
这将基本上检查 users 表中是否存在记录,并排除 user id!= 110 或 email@example.com 的行. em>.自然,我建议您使用 Zend_Db方法(例如quoteIdentifier()
),以便生成完全转义的查询表达式.
This will basically check if a record exists in the users table, and exclude rows where user id != 110 or email@example.com. Naturally, I recommend you to use Zend_Db methods such as quoteIdentifier()
in order to generate a fully escaped query expression.
您当然可以添加任意多个字段.
You can add as many fields as you want of course.
您可以找到有关
这篇关于如何修改zend Db_RecordExists验证器的where子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!