7913 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Filtering / Searching DataObjects in the Front End
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
| Go to End | Next > | |
| Author | Topic: | 1266 Views |
-
Filtering / Searching DataObjects in the Front End

28 February 2012 at 6:35am
Hi guys,
I assumed this would be easy, but am really struggling to find info in these forums...
I have "Apartments" stored as data objects:class Apartment extends DataObject {
//db fields
static $db = array (
'Type' => 'Text',
'Title' => 'Varchar(255)',
'Price' => 'Text',
'Rooms' => 'Text',
'Bathrooms' => 'Text',
'Description' => 'Text',
'URLSegment' => 'Varchar(255)',
'OwnerName' => 'Varchar(255)',
'isActive' => 'Boolean',
);Now I have a page that currently just lists all of the apartments in a nice template:
public function listAll ($filter = null){
if(!isset($_REQUEST['start'])) $_REQUEST['start'] = 0;
$filter = "isActive = '1'";
if(!$filter){ return false; } else
$limit = $_REQUEST['start'].",8";
$sort = 'Created ASC';
return DataObject::get('Apartment', $filter, $sort, null, $limit);
}I'd just like to have the ability in the front end for users to filter the results by the attributes of each apartment, E.G - the apartment type, the price range, number of bedrooms etc.
I'm sure it's easy and I'm being a newb, but can somebody please help?
An exmaple form code would be amazing.
cheers!
-
Re: Filtering / Searching DataObjects in the Front End

29 February 2012 at 2:49am
****UPDATE****
OK, I can get one filter to work from the URL paramter:
public function listAll(){
if(!isset($_REQUEST['start'])) $_REQUEST['start'] = 0;
if(isset($_GET['isActive'])) {
$isActive = Convert::raw2sql($_GET['isActive']);
}
else {
$isActive = '0';
}
$filter = "isActive = '" . $isActive . "'";
$limit = $_REQUEST['start'].",8";
$sort = 'Created ASC';
return DataObject::get('Apartment', $filter, $sort, null, $limit);
}and then I just have radio buttons etc onthe listing page which append parameters to the URL when clicked. I think this is on the right track?
can anybody shed any light on how I can add multiple filters?
What AAB did here: http://www.mymuswell.com/places/restaurants-in-muswell-hill/?type=19
is pretty much EXACTLY what I'm trying to achieve, please can anybody help??thanks!
-
Re: Filtering / Searching DataObjects in the Front End

29 February 2012 at 7:50am
can anyone shed any light on this AT ALL? am I even approaching this correctly or way of?
to clarify, my problem is this:
I have a website of holiday homes (Apartments)
each apartment is a dataobject
each apartment has lots of "amenities" which are also dataobjectsI can do something like this: $filter = "Price < " . $maxprice . "
or $filter = "AmenityID='1'"
which easily filters my page of apartments to just those with a maxprice that is set by the page URL - E.G - apartments?maxprice=100 or just those which are related to an Amenity with an ID of 1.but how do i filter out using lots of options? E.G - I want users to be able to filter apartments that have a BBQ and a POOL
my "amenities" are in a seperate table: Amenity. And the relationship for "Apartment" to "Amenity" is $many_many
the relationship of "Amenity" to "Apartment" is $belongs_many_manythat all works fine, when creating "Apartment" DataObject I can specify what "Amenity" it has, and the links are seutp in the DB
but how can a user filter this data in the front end?
Basically, I want to be able to do this www.mysite.com/apartments?AmenityID=1,3,5,7,12
(similar to what AAB did here: http://www.mymuswell.com/places/restaurants-in-muswell-hill/?type=21&facility=66%2C23"
and that will display a list of apartments that have amenities with IDs of 1 AND 3 AND 5 AND 7 AND 12
at the moment, I can only do one amenity at a time... -
Re: Filtering / Searching DataObjects in the Front End

29 February 2012 at 8:20am
some progress here:
$AmenityIDs = array(1,2,4);
$AmenityIDs = implode(',', $AmenityIDs);
$filter = "Price < $maxprice AND Price > $minprice AND isActive = $isActive AND AmenityID IN ($AmenityIDs)";
Debug::show($filter);
$sort = 'Created ASC';
$join = 'LEFT JOIN `Apartment_Amenities` ON `ApartmentID` = `Apartment`.ID';
$limit = $_REQUEST['start'].",10";but that is just saying if the AmenityID is listed ANYWHERE in the array, then return the apartment, but I need it the apartment to match ALL of the IDs in the array, does that make sense?
-
Re: Filtering / Searching DataObjects in the Front End

14 April 2012 at 12:26am
Hi CHD,
Did you have any luck with this? I'm looking to do something very similar within the eCommerce module.
Cheers
-
Re: Filtering / Searching DataObjects in the Front End

14 April 2012 at 12:51am
hi there
have you had a look at the code in generic-views? or probable you can even just use generic views. once i've used smth like this - it worked but as far as i remember i had some troubles filtering many_many's.
http://www.silverstripe.org/generic-views-module/
best
lukas -
Re: Filtering / Searching DataObjects in the Front End

14 April 2012 at 1:20am
I tried a few different methods and never REALLY got to what I wanted.
I'm currently playing around with extending the SS search function, but this is a personal project so I don't get much time to work on it!
I might give up soon and pay one of my devs to sort it for me! Happy to share the results if/when i do...here's the latest progress/attempt:
public function SearchForm() {
$context = singleton('Apartment')->getCustomSearchContext();
$extraFields = $context->getSearchFields();
$fields = new FieldSet(
new DropdownField(
'RoomsMin', 'Min Rooms', array( '' => 'Any', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '10' => '10')),
new DropdownField(
'RoomsMax', 'Max Rooms', array( '' => 'Any', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '10' => '10')),
new DropdownField(
'TownID',
'Choose a town',
Dataobject::get("Town")->toDropdownMap("ID", "Name", "Please Select")),
new DropdownField(
'Amenity.ID',
'Choose amenities',
Dataobject::get("Amenity")->map("ID", "Name", "Please Select"))
);
$form = new Form($this, "SearchForm",$fields,
new FieldSet(
new FormAction('doSearch', 'Search')
)
);return $form;
}
public function doSearch($data, $form) {
$context = singleton('Apartment')->getCustomSearchContext();
//Add fields so that only published are shown.
//$data['isActive'] = '1';
$results = $context->getResults($data);
return $this->customise(array(
'Results' => $results
))->renderWith(array('SearchPage_results', 'Page'));
} -
Re: Filtering / Searching DataObjects in the Front End

11 July 2012 at 8:42am
hey ryan
did you made some progress? i had to ;-) i've adapted your code +/- to the solution i came up with. now many_manys are working fine and i've implemented autocomplete with Martimiz field. you can have a play with it and grap the source on http://search.kraftausdruck.ch/apartmentseach
best
lukas
| 1266 Views | ||
| Go to Top | Next > |



