Skip to content

Instantly share code, notes, and snippets.

@darrylhein
Last active January 2, 2016 18:59
Show Gist options
  • Save darrylhein/8347340 to your computer and use it in GitHub Desktop.
Save darrylhein/8347340 to your computer and use it in GitHub Desktop.
Search for sets in Kohana/XM ORM
<?php
/**
* Also used by ORM_Radios & ORM_YesNo & ORM_Gender (through ORM_Radios)
*
* @param mixed $column_name
* @param mixed $value
* @return array
*/
public static function search_prepare($column_name, $value, array $options = array(), ORM $orm_model = NULL) {
$methods = array();
$sql_table_name = ORM_Select::get_sql_table_name($orm_model);
// nothing received or not an array or all is in the array so don't do anything with this field
if (empty($value) || ! is_array($value) || in_array('all', $value)) {
// don't do anything, default $methods array is good
// none is in the array so search for anything not set (0 or NULL)
} else if (in_array('none', $value)) {
$methods = array(
// add clause to check for anything set to 0
array(
'name' => 'where',
'args' => array($sql_table_name . $column_name, '=', DB::expr("''")),
),
);
} else {
foreach ($value as $_value) {
$methods[] = array(
'name' => 'or_where',
'args' => array(DB::expr('FIND_IN_SET(' . Database::instance()->quote($value) . ', ' . Database::instance()->quote_column($sql_table_name . '.' . $column_name) . ')'), '>', 0),
);
}
}
return $methods;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment