Gossamer Forum
Home : Products : Gossamer Links : Discussions :

query_sth problem

Quote Reply
query_sth problem
Hi all,

I have a new table linked to links table and want links to search that table. So I pass a hash ref of $args to the query_sth method. the $args basically contains parameters similiar to the $args for links table search, but without fields specific to links table. Strangly the search returns all record in my table. I checked the debug message, the actual sql command is "SELECT * From mytable" with the "WHERE" clause missing! If I extract the search terms from the $args hash and pass them to query_sth like this: query_sth('MyField_name'=>$terms), I can get what I expected.

What's wrong in this case? How can I specify column(s) to be searched in the $args hash?

Thank you in advance for your help.

Long
Quote Reply
Re: [long327] query_sth problem In reply to
What is the actual codes you are using? You would need something like;

Code:
$sth = $DB->table('mytable')->select( { MyFieldName => $terms } ) || die $GT::SQL::error;

while (my $hit = $sth->fetchrow_hashref) {
.... do stuff here
}

Be aware that 'mytable' must appear in LSQL format... i.e with a prefix, such as lsql_mytable... it must also have a .def file in the /admin/defs/ folder. Without this, it would normally give an error.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [long327] query_sth problem In reply to
Hi,

Sounds like $args isn't correct. You'll get a 'select * from table' if there were no valid column names found in $args.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] query_sth problem In reply to
Hi Alex,

thank you for reply.

Do you mean for a customer table you have to specify the column to search? When I search my own table, I used the same $agrs passed to query_sth for searching links table but with some parameters removed such as isValidate=>Yes. That's why I got all records in the search result?

If I want to pass $args to query_sth instead of using query_sth('fieldname'=>$terms), how can I specify the column in the $args? something like $args->{field}='column'? The reason I want to use $args is to pass other parameters such as sb, mh...

Long
Quote Reply
Re: [long327] query_sth problem In reply to
Hi,

Post a sample of what you are trying to do and what is in $args. That will clear things up.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] query_sth problem In reply to
Hi Alex,

Here is the code used in search.pm

Code:
my $cache_db = $DB->table ('My_Table');
my $args; # We need a query or we can't search for anything.
#$args->{query} = $IN->param('query') or return $results;
$args->{query} = $query->{query};
$args->{bool} = (defined $query->{bool} and $query->{bool} =~ /^(and|or)$/i) ? uc $1 : $CFG->{search_bool};
$args->{substring} = defined $query->{substring} ? $query->{substring} : $CFG->{search_substring};
$args->{so} = (defined $query->{so} and $query->{so} =~ /^(asc|desc)$/i ? $1 : '');
$args->{sb} and ($query->{sb} =~ /^[\w\s,]+$/ or ($query->{sb} = '')); # do the search
#my $cache_sth = $cache_db->query_sth('Page'=>$args->{query}); # this works, 'Page' is a column name
my $cache_sth = $cache_db->query_sth($args); # This won't work, but returns all records.


the dumped $args likes this:

$VAR1 = {
'so' => 'ASC',
'bool' => 'AND',
'filter' => undef,
'query' => 'my query terms',
'substring' => '0'
};

Thank you for your help.

Long
Quote Reply
Re: [long327] query_sth problem In reply to
Hi,

Ah, I see. You need to make sure you have defined which columns you want to be searched via query. Make sure one or more of your columns have a weight tag set in the .def file, and that the weight is > 0.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] query_sth problem In reply to
Hi Alex,

Now it works the right way. Thank you very much.

I have another question. Is it possible to write a plugin (without modifying search.pm) to allow searching an additional table and return the results with links results? It seems to me there is no such hooks and modifying the search.pm file is the only way.

Long
Quote Reply
Re: [long327] query_sth problem In reply to
You can write a plugin which has a PRE hook on search_results.