Elo bird lovers!
Is there any way to pass in an associative array as conditions for find / findFirst? I've found no clue in the forums/docs/source...
You can already bind parameters like so:
$conditions = ['type_id'=>42, 'enabled'=>1];
$robot = Robot::findFirst([
'conditions' => 'type_id=:type_id: AND enabled=:enabled:',
'bind' => $conditions,
]);
I was wondering if you could do something like (laziness rulz):
$conditions = ['type_id'=>42, 'enabled'=>1];
$robot = Robot::findFirst([
'conditions' => $conditions,
]);
So if the conditions
is an array, it expands automagically to the correct string and bind
?
I could also write a static method, but would rather use the built-in features if there is such :]
class MyModel extends Phalcon\Mvc\Model {
public static function arrayConditions(array $conditions, array $parameteres=[]) {
if(count($conditions)>0) {
$c = '';
foreach($conditions as $field=>$value) $c.= ' AND '.$field.'=:'.$field.':';
$c = substr($c,5);
$parameters = array_merge($parameters, [
'conditions' => $c,
'bind' => $conditions,
]);
}
return $parameters;
}
}
$conditions = ['type_id'=>42, 'enabled'=>1];
$robot = Robot::findFirst(MyModel::arrayConditions($conditions));
Cheers!