Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's better way to build NSPredicate with to-many deep relationships?

Tags:

I have three entities: EntityA, EntityB and EntityC connected with to-many relationships.

See schema for details:

alt text http://img706.imageshack.us/img706/9974/screenshot20091220at124.png

For getting all instance of EntityA which depend from EntityB.name I use the predicate like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY EntityB.name like 'SomeName'"];

What should be predicate for getting all instance of EntityA which depend from EntityC.name? I tried query like @"ANY EntityB.entitiesC.name like 'SomeName'" but get exception "multiple to-many keys not allowed here".

Best regards,

Victor

like image 395
Victor Avatar asked Jan 05 '10 15:01

Victor


1 Answers

My final solution is to use SUBQUERY.

NSPredicate *p = [NSpredicate predicateWithFormat:@"(name like %@) AND (0 != SUBQUERY(entitiesB, $x, (0 != SUBQUERY($x.entitiesC, $y, $y.name like %@).@count)).@count)", nameA, nameC];

Unfortunately I was unable to expand this query on nsExpression objects.

like image 147
Victor Avatar answered Nov 24 '22 06:11

Victor