[Dbix-class] adding conditions

Fernan Aguero fernan at iib.unsam.edu.ar
Tue Jan 9 20:07:56 GMT 2007


+----[ Octavian Rasnita <orasnita at gmail.com> (09.Jan.2007 16:20):
|
| From: "Oliver Gorwits" <oliver.gorwits at oucs.ox.ac.uk>
| 
| >>How can I add these conditions after defining $rs?
| >
| >$rs = $rs->search({id_user => $id_user});
| 
| Thank you. This way it works well. I have tried:
| 
| $rs->{cond}->{id_user} = $id_user;
| 
| And it works, but only when some conditions are previously defined.
|
+----]

I don't know if $rs->{cond} is an internal structure. If it
is, you don't want to mess with it.

But I guess I know where you're trying to go ...

Do notice that the first element passed to $rs->search 
is a hashref. You can build this hash or hashref yourself
along the way (for example while parsing some input file)
and then submit the conditions to your search.

If you also build your search attributes into a separate
structure, this also makes for a shorter (and easier to
read) call to ->search().

my %where;

$where{user_id} = 1;
$where{name} = { 'like', '%fernan%' };
$where{lastname} = { '!=', 'Rasnita' };
$where{weight} = { '<=', '100' }; 

my %attributes;

$attributes{order_by} = 'weight DESC';
$attributes{distinct} = 1;
$attributes{columns} = [ qw/ user_id name / ];

my $rs = $model->search( \%where, \%attributes );

Fernan

PS: I like to use %where for this in my code, cause
it makes it clear that everything you put there will be
part of the SQL WHERE clause.





More information about the Dbix-class mailing list