[Dbix-class] fetching a column value directly
hello, I have this host table, which has all unique records. So i want to get a specific column value for a specific host. i am doing below, but i think it is not efficient. is there a better way? my $hostAdd_rs = $schema-resultset('Host')-search({host=$host},{select=[qw/addr/]}); while (my $rec = $hostAdd_rs-next() ) { $hostAdd = $rec-get_column('addr'); } can we not do it in a single line command? without any while loop? i tried, but getting various errors. ty. Rajeev ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
Re: [Dbix-class] fetching a column value directly
1. Define column host as unique in your Result pm, then you can use $rs-find({ host = $host })-addr; 2. Use method first() $hostAdd = $rs-search({host = $host})-get_column('addr')-first; On Mon, Nov 4, 2013 at 2:29 PM, Rajeev Prasad rp.ne...@yahoo.com wrote: hello, I have this host table, which has all unique records. So i want to get a specific column value for a specific host. i am doing below, but i think it is not efficient. is there a better way? my $hostAdd_rs = $schema-resultset('Host')-search({host=$host},{select=[qw/addr/]}); while (my $rec = $hostAdd_rs-next() ) { $hostAdd = $rec-get_column('addr'); } can we not do it in a single line command? without any while loop? i tried, but getting various errors. ty. Rajeev ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
Re: [Dbix-class] fetching a column value directly
Your code is correct. `search` may return several results, so you must either iterate over them or assign/do something with all the results, e.g. -search(…)-all returns a list of Host resultrow objects. If you are sure the hosts is unique and therefore `search` will only find one result, you can simply write: my $host_addr = ………-search(…)-first-addr Note the automatically generated accessor for the column. signature.asc Description: PGP signature ___ List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class IRC: irc.perl.org#dbix-class SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk