I'm looking for following SQL equivalent in Idiorm and Paris:
SELECT * FROM table WHERE (regex=0 AND col=_match_) OR (regex=1 AND _match_ REGEXP col)
LIMIT 1
Following Idiorm & Paris statements only matches col=_match_
:
Idiorm:
$row = ORM::for_table('table')
->where_equal('col', '_match_')
->find_one()
Paris:
MyModel::where('col', '_match_')->find_one()
After reading docs and codes, it seems that Idiorm/Paris doesn't support that functionality. So, we have two options:
Using where_raw()
method, which by this way we will pass where clause as a string directly to Idiorm.
Adding a new method like where_condition()
to Idiorm ORM class which matches $value against $column_name by setting $reverse = true
, otherwise, it's a normal where condition (that matches $column_name against $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;
}