Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Help with basic table - managed via modelAdmin - with display limit & multiple page results


Reply

36 Posts   2525 Views

Avatar
CHD

28 August 2010 at 2:50am Community Member, 218 Posts

this:
Director::set_environment_type('dev');

goes in mysite>config right?

and yeah i noticed you used a generic term, but i wasnt sure what im supposed to replace it with??
i didn't realise that would throw up an error (d'oh!)

Avatar
UncleCheese

28 August 2010 at 3:17am 4085 Posts

Yup, mysite/_config.php.

You can replace that filter clause with whatever database fields you want to search on..

Avatar
CHD

28 August 2010 at 3:18am Community Member, 218 Posts

OK thanks, i'll have to see if i can work out what the fields are named!

Avatar
CHD

29 August 2010 at 2:43am Community Member, 218 Posts

got it working!!
i added the column name "Title" and now it searches perfectly, and the results are pretty much EXACTLY how i was hoping!

one last thing, how do i search more than one table column?
ideally, I'd like to search the whole table, is there a way i can name the table rather than every column?

here's mu updated function:

public function doProductSearch($data, $form) {
if(isset($data['s'])) {
$filter = "Title LIKE '%".Convert::raw2sql($data['s'])."%'";
}

the table itself is called Product, but when i tried that it threw up the same error as before.

Avatar
CHD

29 August 2010 at 3:10am Community Member, 218 Posts

oh and also, when i try and view the 2nd page of results from this product search, i get this error:
SecurityID doesn't match, possible CSRF attack.

i've had this before with a hard coded form and i had to switch off sitewide SecurityIDs, which isnt really ideal!
can you help me with fixing this problem? thanks!

Avatar
UncleCheese

29 August 2010 at 3:42am 4085 Posts

Multiple filters:

I would first define an array $searchable_fields on your Product object. Then:

public function doProductSearch($data, $form) {
if(isset($data['s'])) {
$filters = array();
foreach(Product::$searchable_fields as $field) {
$filters[] = "$field LIKE '%".Convert::raw2sql($data['s'])."%'";
}
$filter = implode(" OR ", $filters);
// etc...
}

For the security ID issue, that's going to be a problem. The pagination isn't posting the form params with each page, which creates more issues than just the security ID.. I think you can solve that by setting your form method to "get", so the params are posted for every page through the URL.

public function ProductSearchForm() {
$f = new Form (
$this,
"ProductSearchForm",
new FieldSet(new TextField('s','')),
new FieldSet(new FormAction('doProductSearch','Search'))
);
$f->setFormMethod('get');
reutrn $f;
}

Avatar
CHD

29 August 2010 at 3:48am (Last edited: 29 August 2010 3:49am), Community Member, 218 Posts

OK that code has confused me a little, and as is isnt that important, i'll leave that out!
the security ID resolution worked perfectly though! (except for a typo on the word "return")

thanks for all your help!!

see it in action here:
http://www.worldaircraftsolutions.com.sg/catalogue/

Avatar
CHD

29 August 2010 at 4:05am Community Member, 218 Posts

OK last question... i've set the field value to "Search by title"
but is there a way i can make that value become blank on focus?
usually i'd set these things in the form coding, but when forms are created via the .php files like this im not sure how to add variables like that!

usually i'd use this:
onClick="this.value=''"

but how do i implement that into a form when the form is generated by PHP like this? (amateur question im sure!)