Snippets - Little bits of code to make you happy
Getting items from a has_many or many_many relationTweet
7 October 2010 | | | Supports v2.4, v2.3
Have you ever found yourself using DataObject::get() to fetch objects in a has_many so that you can filter/sort them easily?
Well as it happens there is no need to do this thanks to a totally undocumented bit of code!
Lets say you have this relationship on your Page:
static $has_many = array( 'MyItems' => 'DataObject' );
you can do this (from with the context of your Page object):
$this->MyItems("FieldA = 'B'", "Field DESC", NUll, 2);
instead of using something like
DataObject::get("MyItems", "PageID = " . $this->ID . " AND FieldA = 'B'", "FieldB DESC", NUll, 2);
This can be especially useful when you have interrelating relationships, it will save you having to use a manual join.
Special thanks to Daniel Hensby for finding this code and passing it on!
Special thanks go to Daniel Hensby for their contributions to this post.