Assume there exist following InfoBase classes and related tables with 'pf_' prefix and the $key is interpreted by MySqlDataProvider:
BookInfo : books (id, id_author, title)
PublisherInfo : publishers (id, name)
ReleaseInfo : releases (id, id_book, id_publisher, released)
PersonInfo : persons (id, first_name, last_name)
1. Query authors of books containing 'animal' in their titles:
$rel = new SearchRelation ('id', 'BookInfo', 'id_author');
$key = new RelatedKey($rel, 'title', 'animal', SearchKey::CMP_LIKE, true);
$list = MyDataProvider::instance()->ListItems('PersonInfo', $key);
The $key query will be interpreted as
SELECT DISTINCT r0.∗ FROM pf_persons AS r0
INNER JOIN pf_books r1 on r0.`id`=r1.author_id
WHERE r1.`title` LIKE '%animal%'
GROUP BY r0.`id`;
2. Query publishers of books containing 'animal' in their titles:
$rel = array (
new SearchRelation ('id', 'ReleaseInfo', 'id_publisher'),
new SearchRelation ('id_book', 'BookInfo', 'id'),
);
$key = new RelatedKey($rel, 'title', 'animal', SearchKey::CMP_LIKE, true);
$list = MyDataProvider::instance()->ListItems('PublisherInfo', $key);
The $key query will be interpreted as
SELECT DISTINCT r0.∗ FROM pf_publishers AS r0
INNER JOIN pf_releases r1 on r0.`id`=r1.`id_publisher`
INNER JOIN pf_books r2 on r1.`id_book`=r2.`id`
WHERE r2.`title` LIKE '%animal%'
GROUP BY r0.`id` ;
3. Query authors with first_name 'John' publishing before January 1st, 2000 and sort them by first publishing date:
$rel = array (
new SearchRelation ('id', 'BookInfo', 'id_author'),
new SearchRelation ('id', 'ReleaseInfo', 'id_book'),
);
$relKey = new RelatedKey($rel, 'released', '2000-1-1', SearchKey::CMP_LESS);
$relKey->sortBy = 'MIN(released)';
$key->AddAndKey ($relKey);
$list = MyDataProvider::instance()->ListItems('PersonInfo', $key);
The $key query will be interpreted as
SELECT DISTINCT r0.∗ FROM pf_persons AS r0
INNER JOIN pf_books r1 on r0.`id`=r1.`id_author`
INNER JOIN pf_releases r2 on r1.`id`=r2.`id_book`
WHERE r0.`first_name` LIKE 'john'
AND r2.`released` < '2000-1-1'
GROUP BY r0.`id`
ORDER BY MIN(r2.`released`);