Select 在 Idiorm 和 Paris 中按正则表达式匹配的行
Select row by regex match in Idiorm and Paris
我正在寻找 Idiorm 和 Paris 中的以下 SQL 等价物:
SELECT * FROM table WHERE (regex=0 AND col=_match_) OR (regex=1 AND _match_ REGEXP col)
LIMIT 1
以下 Idiorm 和 Paris 语句仅匹配 col=_match_
:
成语:
$row = ORM::for_table('table')
->where_equal('col', '_match_')
->find_one()
巴黎:
MyModel::where('col', '_match_')->find_one()
句子where_any就是那个的解决方案
示例:
$myModel = ORM::for_table('table')
->where_any_is(array(
array('regex'=> 0,'col' => 'match'),
array('regex'=> 1,'col' => '_match_')))
->limit(1)->find_many();
查看过滤查询的官方文档:https://idiorm.readthedocs.org/en/latest/querying.html#filtering-results
阅读文档和代码后,Idiorm/Paris 似乎不支持该功能。所以,我们有两个选择:
使用where_raw()
方法,通过这种方式我们将where子句作为字符串直接传递给Idiorm。
在 Idiorm ORM class 中添加一个像 where_condition()
这样的新方法,它通过设置 $reverse = true
来匹配 $value 与 $column_name,否则,它是一个正常的 where 条件(匹配 $column_name 对 $value)。
public function where_condition($column_name, $operator, $value, $reverse = false)
{
$multiple = is_array($column_name) ? $column_name : array($column_name => $value);
$result = $this;
foreach($multiple as $key => $val) {
// Add the table name in case of ambiguous columns
if (count($result->_join_sources) > 0 && strpos($key, '.') === false) {
$table = $result->_table_name;
if (!is_null($result->_table_alias)) {
$table = $result->_table_alias;
}
$key = "{$table}.{$key}";
}
$key = $result->_quote_identifier($key);
if($reverse)
$result = $result->_add_condition('where', "? {$operator} {$key}", $val);
else
$result = $result->_add_condition('where', "{$key} {$operator} ?", $val);
}
return $result;
}
我正在寻找 Idiorm 和 Paris 中的以下 SQL 等价物:
SELECT * FROM table WHERE (regex=0 AND col=_match_) OR (regex=1 AND _match_ REGEXP col)
LIMIT 1
以下 Idiorm 和 Paris 语句仅匹配 col=_match_
:
成语:
$row = ORM::for_table('table')
->where_equal('col', '_match_')
->find_one()
巴黎:
MyModel::where('col', '_match_')->find_one()
句子where_any就是那个的解决方案 示例:
$myModel = ORM::for_table('table')
->where_any_is(array(
array('regex'=> 0,'col' => 'match'),
array('regex'=> 1,'col' => '_match_')))
->limit(1)->find_many();
查看过滤查询的官方文档:https://idiorm.readthedocs.org/en/latest/querying.html#filtering-results
阅读文档和代码后,Idiorm/Paris 似乎不支持该功能。所以,我们有两个选择:
使用
where_raw()
方法,通过这种方式我们将where子句作为字符串直接传递给Idiorm。在 Idiorm ORM class 中添加一个像
where_condition()
这样的新方法,它通过设置$reverse = true
来匹配 $value 与 $column_name,否则,它是一个正常的 where 条件(匹配 $column_name 对 $value)。public function where_condition($column_name, $operator, $value, $reverse = false) { $multiple = is_array($column_name) ? $column_name : array($column_name => $value); $result = $this; foreach($multiple as $key => $val) { // Add the table name in case of ambiguous columns if (count($result->_join_sources) > 0 && strpos($key, '.') === false) { $table = $result->_table_name; if (!is_null($result->_table_alias)) { $table = $result->_table_alias; } $key = "{$table}.{$key}"; } $key = $result->_quote_identifier($key); if($reverse) $result = $result->_add_condition('where', "? {$operator} {$key}", $val); else $result = $result->_add_condition('where', "{$key} {$operator} ?", $val); } return $result; }