[TYPO3-mvc] FYI: Implemented another handy query feature
Jochen Rau
jochen.rau at typoplanet.de
Wed Mar 10 15:46:38 CET 2010
Hi.
Wit r1998 of Extbase I have implemented another feature. It is now
possible to write a property path as first operand of a comparison. For
example: Inside the PostRepository you can write
public function findTaggedWith($name) {
$query = $this->createQuery();
$query->matching($query->equals('tags.name', $name));
return $query->execute();
}
This will return all Posts that are tagged with the given name. Extbase
expands this to a INNER JOIN with the Tag table.
This works for 1:1, 1:n, and m:n relations. And even for comma separated
values. And it works for all comparisons (equals, in, contains, like ...).
And you can go deep into the object graph. Inside the BlogRepository you
can write:
public function findBlogsWithPostsTaggedWith($name) {
$query = $this->createQuery();
$query->matching($query->equals('posts.tags.name', $name));
return $query->execute();
}
to retrieve all Blogs that have at least one Post tagged with the given
name. Extbase will expand this recursively to three INNER JOINS (because
of the relation table post-tag). I am happy that I don't have to write
this statement on my own anymore ;-):
SELECT DISTINCT tx_blogexample_domain_model_blog.* FROM
tx_blogexample_domain_model_blog INNER JOIN
tx_blogexample_domain_model_post ON
tx_blogexample_domain_model_blog.uid=tx_blogexample_domain_model_post.blog
INNER JOIN tx_blogexample_post_tag_mm ON
tx_blogexample_domain_model_post.uid=tx_blogexample_post_tag_mm.uid_local INNER
JOIN tx_blogexample_domain_model_tag ON
tx_blogexample_post_tag_mm.uid_foreign=tx_blogexample_domain_model_tag.uid
WHERE tx_blogexample_domain_model_tag.name = 'Tag' AND
tx_blogexample_domain_model_post.deleted=0 AND
tx_blogexample_domain_model_post.t3ver_state<=0 AND
tx_blogexample_domain_model_post.hidden=0 AND
tx_blogexample_domain_model_post.sys_language_uid IN (0,-1) AND
tx_blogexample_domain_model_post.pid IN (105) AND
tx_blogexample_domain_model_tag.deleted=0 AND
tx_blogexample_domain_model_tag.hidden=0 AND
tx_blogexample_domain_model_tag.pid IN (105)
This feature is still non-api but after a discussion with Karsten I am
sure he will implement it, too ;-).
Please test it and report regressions (if any ;-))
Regards
Jochen
More information about the TYPO3-project-typo3v4mvc
mailing list