Gossamer Forum
Quote Reply
Condition object
OK, I have tried several times in different plugins to get a condition object to work, but never can, talk about frustrating. The first time it was throwing an error relating to primary keys, the errors were saying that my table had 4 keys as the primary but I was only providing 2, and in fact it only had 2 - the table on had 3 fields, but that is not my current problem coz I wrote a work around there. Sorry about the whinge but this has been eating at me, has anyone else had problems with the condition object?

Ok, currently what I want to do is delete records from a table if the ID is not located in a list, I have code like this:

$table = $DB->table('myTable');
$condition = GT::SQL::Condition->new('ID', 'NOT IN', @ids);
$table->delete($condition);

where @ids contains the list of ids that I want to keep. I got some error that mentioned my query was of the form 'NOT IN 1 AND 2, 3, 4' .

Can someone perleeeease help me?

Thank you in advance for any feedback as I am at my wits end.

Last edited by:

tommyfotak: Oct 9, 2002, 2:22 PM
Quote Reply
Re: [tommyfotak] Condition object In reply to
I would avoid multiple primary keys, GT::SQL doesn't handle that well and we are looking at removing support for it (almost always better to have an auto_increment and a unique index).

As for your condition problem, it's because:

SELECT * FROM myTable WHERE ID NOT IN (1,2,3)

is not valid SQL. You need to do:

SELECT * FROM myTable WHERE NOT(ID IN (1,2,3))

which translates to:

$condition = GT::SQL::Condition->new('ID', 'IN', \@ids);
$condition->not; # Negates it

or as one line:

$condition = GT::SQL::Condition->new('ID', 'IN', \@ids)->not;

Also, you need to pass an array ref, not an array as the third argument.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Condition object In reply to
Thanks Alex, you are a champion.