[Dbix-class] prefetch problem
Marc Perez
markkus.80 at gmail.com
Fri Aug 13 15:03:04 GMT 2010
Hi,
Thanks very much.
I already solved it. I'm calling $file->fieldfiles->first->name()
I know there will always be a related result.
But you were right fieldfiles is returning a resultset.
Thanks.
Marc
On Fri, Aug 13, 2010 at 4:54 PM, Ronald J Kimball
<rkimball at pangeamedia.com> wrote:
> On Fri, Aug 13, 2010 at 10:39 AM, Marc Perez <markkus.80 at gmail.com> wrote:
>> I'm doing the next query:
>> my $files = $catVar->model('cmsDB::Files')->search(
>> {
>> 'me.cmsobj_id' => $this->cmsobj_id,
>> -or => [mime_type => 'image/jpeg',mime_type =>
>> 'image/gif',mime_type => 'image/png'],
>> 'fieldfiles.language_id' => $langId
>> },
>> {
>> join => 'fieldfiles',
>> prefetch => 'fieldfiles'
>> }
>> );
>>
>> When I try to access the fieldfiles data:
>>
>> while( my $file = $files->next() ) {
>> print($file->fieldfiles->name)
>> }
>>
>> I get the error:
>> Can't locate object method "name" via package "DBIx::Class::ResultSet"
>
> Because "fieldfiles" is a has_many relationship, calling the accessor
> in scalar context returns a resultset object, not a row object. You
> probably want to do one of these:
>
> while (my $file = $files->next) {
> my $fieldfiles = $file->fieldfiles;
> while (my $fieldfile = $fieldfiles->next) {
> print $fieldfile->name;
> }
> }
>
> while (my $file = $files->next) {
> foreach my $fieldfile ($file->fieldfiles) {
> print $fieldfile->name;
> }
> }
>
> You could also do something like this:
>
> while (my $file = $files->next) {
> print join ", ", $file->fieldfiles->get_column('name')->all;
> }
>
> Ronald
>
> _______________________________________________
> 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
>
More information about the DBIx-Class
mailing list