FAQ
Hi,
My name is Marc, I'm not use to write to the list but I have a problem
which I dont know exactly how to solve.
I have this two models:

FIRST:
package cmsDB::Files;
use strict;
use warnings;
use base 'DBIx::Class';
__PACKAGE__->load_components("Core");
__PACKAGE__->table("files");
__PACKAGE__->add_columns(
"file_id",
{ data_type => "INT", default_value => undef, is_nullable => 0, size => 10 },
......
__PACKAGE__->has_many(
"fieldfiles",
"cmsDB::Fieldfiles",
{ "foreign.file_id" => "self.file_id" },
);

SECOND:
package cmsDB::Fieldfiles;
use strict;
use warnings;
use base 'DBIx::Class';
__PACKAGE__->load_components("Core");
__PACKAGE__->table("fieldfiles");
__PACKAGE__->add_columns(
"file_id",
{ data_type => "INT", default_value => undef, is_nullable => 0, size => 10 },
"language_id",
{ data_type => "INT", default_value => undef, is_nullable => 0, size => 10 },
"name",
{
data_type => "VARBINARY",
default_value => undef,
is_nullable => 1,
size => 250,
},
........
__PACKAGE__->set_primary_key("file_id", "language_id");
__PACKAGE__->belongs_to("file_id", "cmsDB::Files", { file_id => "file_id" });
__PACKAGE__->belongs_to(
"language_id",
"cmsDB::Languages",
{ language_id => "language_id" },
);


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"

If I dump $file->fieldfiles:

$Data::Dumper::Indent = 3;
$Data::Dumper::Maxdepth = 3;
Dumper($file->fieldfiles->name)

I get this:

$VAR1 = bless( { '_source_handle' => bless( { 'source_moniker' =>
'Fieldfiles', 'schema' => bless( { 'class_mappings' =>
'HASH(0xa521c78)', 'source_registrations' => 'HASH(0xa5220c8)',
'storage' => 'DBIx::Class::Storage::DBI::mysql=HASH(0xa52e4b8)' },
'cmsDB' ) }, 'DBIx::Class::ResultSourceHandle' ), '_in_storage' => 1,
'_column_data' => { 'link' => '', 'language_id' => '1', 'name' =>
'Jam?n y Queso - Botiga', 'file_id' => '5', 'description' => '' } },
'cms::Model::cmsDB::Fieldfiles' );


Maybe Im doing something wrong...
I would really appreciate any help.

Thanks

Marc P?rez

Search Discussions

  • Seasprocket at Aug 12, 2010 at 4:31 am
    I am a DBIC beginner, but here's what I have learned: Your table classes
    should subclass DBIx::Class::Core (not DBIx::Class). And if you have methods
    that need to be called from a ResultSet class, then you should add also
    create Schema classes that subclass ResultSet.
    On Wed, Aug 11, 2010 at 12:23 PM, Marc Perez wrote:

    Hi,
    My name is Marc, I'm not use to write to the list but I have a problem
    which I dont know exactly how to solve.
    I have this two models:

    FIRST:
    package cmsDB::Files;
    use strict;
    use warnings;
    use base 'DBIx::Class';
    __PACKAGE__->load_components("Core");
    __PACKAGE__->table("files");
    __PACKAGE__->add_columns(
    "file_id",
    { data_type => "INT", default_value => undef, is_nullable => 0, size => 10
    },
    ......
    __PACKAGE__->has_many(
    "fieldfiles",
    "cmsDB::Fieldfiles",
    { "foreign.file_id" => "self.file_id" },
    );

    SECOND:
    package cmsDB::Fieldfiles;
    use strict;
    use warnings;
    use base 'DBIx::Class';
    __PACKAGE__->load_components("Core");
    __PACKAGE__->table("fieldfiles");
    __PACKAGE__->add_columns(
    "file_id",
    { data_type => "INT", default_value => undef, is_nullable => 0, size => 10
    },
    "language_id",
    { data_type => "INT", default_value => undef, is_nullable => 0, size => 10
    },
    "name",
    {
    data_type => "VARBINARY",
    default_value => undef,
    is_nullable => 1,
    size => 250,
    },
    ........
    __PACKAGE__->set_primary_key("file_id", "language_id");
    __PACKAGE__->belongs_to("file_id", "cmsDB::Files", { file_id => "file_id"
    });
    __PACKAGE__->belongs_to(
    "language_id",
    "cmsDB::Languages",
    { language_id => "language_id" },
    );


    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"

    If I dump $file->fieldfiles:

    $Data::Dumper::Indent = 3;
    $Data::Dumper::Maxdepth = 3;
    Dumper($file->fieldfiles->name)

    I get this:

    $VAR1 = bless( { '_source_handle' => bless( { 'source_moniker' =>
    'Fieldfiles', 'schema' => bless( { 'class_mappings' =>
    'HASH(0xa521c78)', 'source_registrations' => 'HASH(0xa5220c8)',
    'storage' => 'DBIx::Class::Storage::DBI::mysql=HASH(0xa52e4b8)' },
    'cmsDB' ) }, 'DBIx::Class::ResultSourceHandle' ), '_in_storage' => 1,
    '_column_data' => { 'link' => '', 'language_id' => '1', 'name' =>
    'Jamón y Queso - Botiga', 'file_id' => '5', 'description' => '' } },
    'cms::Model::cmsDB::Fieldfiles' );


    Maybe Im doing something wrong...
    I would really appreciate any help.

    Thanks

    Marc Pérez

    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive:
    http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/


    --
    Bikewise: http://www.bikewise.org
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20100811/3ebd4483/attachment.htm
  • Marc Perez at Aug 12, 2010 at 7:04 am
    Hi,
    Thanks for your reply. Is not __PACKAGE__->load_components("Core")
    adding the functionality of DBiX::Class::Core?
    Yes, I have methods called from a Resultset, but the work fine.

    Marc
  • Marc Perez at Aug 13, 2010 at 11:51 am
    Hi,

    Do you thinks is there another possibility to find a solution o test
    other aspects to solve this?

    Thanks very much

    Marc
    On Thu, Aug 12, 2010 at 9:04 AM, Marc Perez wrote:
    Hi,
    Thanks for your reply. Is not __PACKAGE__->load_components("Core")
    adding the functionality of DBiX::Class::Core?
    Yes, I have methods called from a Resultset, but the work fine.

    Marc
  • Jay Shirley at Aug 13, 2010 at 1:25 pm

    On Fri, Aug 13, 2010 at 4:51 AM, Marc Perez wrote:
    Hi,

    Do you thinks is there another possibility to find a solution o test
    other aspects to solve this?

    Thanks very much

    Marc
    First, there's a DBIx::Class mailing list for DBIC questions, which
    this is. It has nothing to do with Catalyst and is better asked
    there.

    Second, you are calling a method on a result set. Not a row, so
    ->name wouldn't work.

    while( my $file = $files->next() ) {
    # $files is a resultset, iterate and get $file which is a row

    # Now look at the 'has_many'.
    my $fieldfiles = $file->fieldfiles; # this is 'has_many', so it
    returns a resultset
    while ( my $fieldfile = $fieldfiles->next ) {
    # Now you have a row, so you can call methods
    print $fieldfile->name;
    }
    }

    Any follow up questions would be best directed to the DBIC mailing
    list, however.
    http://lists.scsys.co.uk/mailman/listinfo/dbix-class

    Thanks,
    -Jay
  • Marc Perez at Aug 13, 2010 at 2:43 pm
    Hi Jay,

    Ok, sorry, I will write next time to the DBIX list.
    You were right, I was calling name() on a resultset, I had to call
    first()->name() and works.

    Thanks very much

    Marc
  • Tomas Doran at Aug 13, 2010 at 2:51 pm

    On 13 Aug 2010, at 15:43, Marc Perez wrote:

    Hi Jay,

    Ok, sorry, I will write next time to the DBIX list.
    DBIX is a namespace which contains many projects.

    You mean DBIx::Class, or DBIC for short.

    Cheers
    t0m
  • Marc Perez at Aug 13, 2010 at 2:55 pm
    Hi,

    Yes, I mean DBIX::Class. I've already subscribed to the list.

    Thanks

    Marc
    On Fri, Aug 13, 2010 at 4:53 PM, Tomas Doran wrote:
    On 13 Aug 2010, at 15:43, Marc Perez wrote:

    Hi Jay,

    Ok, sorry, I will write next time to the DBIX list.
    DBIX is a namespace which contains many projects.

    You mean DBIx::Class, or DBIC for short.

    Cheers
    t0m


    _______________________________________________
    List: Catalyst@lists.scsys.co.uk
    Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
    Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
    Dev site: http://dev.catalyst.perl.org/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedAug 11, '10 at 7:23p
activeAug 13, '10 at 2:55p
posts8
users4
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase