17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 2670 Views |
-
How to use ComponentSet?

24 October 2008 at 10:53am Last edited: 24 October 2008 11:12am
Books can have many categories. Categories can have many books.
I'd like to return a list of books from a specific category.
I have Book.php (extends Page), Category.php (extends DataObject).
BookHolder.php Controller
function getBooksByCategory($categoryID){
$whereStatement = "CategoryID ='". $categoryID ."'";
return DataObject::get("Books", $whereStatement); // Do I replace DataObject with ComponentSet?
}The ComponentSet page in the documentation doesn't explain much.
I read that ComponentSet has to do with Many_Many relationships.
Any examples are greatly appreciated!
Thanks!
-
Re: How to use ComponentSet?

24 October 2008 at 6:51pm
This did the trick
function getBooksByCategory($categoryID){
$ByCategoryID = DataObject::get_one('Category', 'ID="'.$categoryID.'"');
if($ByCategoryID) {
return $ByCategoryID->getManyManyComponents('Books');
} -
Re: How to use ComponentSet?

27 October 2008 at 1:44pm Last edited: 27 October 2008 1:45pm
This is much cleaner
return $ByCategoryID->Books();
| 2670 Views | ||
|
Page:
1
|
Go to Top |


