I am trying to check that a combination of entries across five columns (I have parent1, parent2, parent3, parent4, and parent5 all of which are integers) does not already exist in my form validation. I tried开发者_Go百科:
$query = Doctrine_Query::create()
->select('m.id', 'm.name') -> from ('Mix m')
->where('m.parent1=?', $values['parent1'])
->orWhere('m.parent1=?', $values['parent2'])
->orWhere('m.parent1=?', $values['parent3'])
->orWhere('m.parent1=?', $values['parent4'])
->orWhere('m.parent1=?', $values['parent5'])
->andWhere('m.parent2=?', $values['parent1'])
->orWhere('m.parent2=?', $values['parent2'])
...and so on. Probably not the most efficient way, but regardless it is throwing an error if one of the values matches any of the columns already in the database, it's not checking for the actual combination... Any help would be very appreciated!
How about...
// Set of possible matches
$set = array($values['parent1'], $values['parent2'], $values['parent3'], $values['parent4'], $values['parent5']);
// Let's make it two-dimensional for simpler DQL syntax
$sets = array($set, $set, $set, $set, $set);
// Multiple WHERE IN AND
$query = Doctrine_Query::create()
->select('m.id', 'm.name')
->from ('Mix m')
->where('m.parent1 IN ? AND m.parent2 IN ? AND m.parent3 IN ? AND m.parent4 IN ? AND m.parent5 IN ?', $sets);
The WHERE IN AND
should make sure each value is matched. However, at least one fail case that comes to mind is that if multiple values in the $set are the same (e.g. '5, 5, 1, 4, 1', the query will give false matches. Maybe someone else can improve on that. Might be a slow query as well.
精彩评论