[Dbix-class] Is the 'as' attribute to a search method broken?

Jonathan Rockway jon at jrock.us
Mon Oct 29 02:56:40 GMT 2007


Randy Moore wrote:
> Hi,
>
> I'm very new with DBIx-Class, but I don't think I'm doing anything
> wrong <grin>.  It looks like the 'as' attribute to a search is not
> working.
> I'm running inside of Catalyst BTW, and I've created a trivial example
> that I think should work.
>
>        my $rs =3D $c->model('PjAppDB::Categories')->search(undef, {
>                select =3D> [
>                  'name',
>                  { length =3D> 'name' }
>                ],
>                as =3D> ['name', 'namelength'],
>        });
>        my $result =3D $rs->first();
>
> My Categories model class *does* have a column named 'name'.
>
> This code is producing:   SELECT name, LENGTH( name ) FROM categories me
> But I think it should produce:  SELECT name name, LENGTH( name )
> namelength FROM categories me
>
> The SQL query that it does produce works perfectly, I just don't see
> how I can access the result for the LENGTH.
> I was expecting to use $result->get_column('namelength')
>
> Am I missing something blindingly obvious? =


I'm not really sure what you're trying to do... but keep in mind that
"as" has nothing to do with the SQL produced.  The "as" name is what you
pass to "get_column" when you want that data.  An example:

   my $result =3D $foo_rs->search({}, {
       select =3D> [{ COUNT =3D> 1 }],
       as       =3D> 'foos',
   });

This will produce something equivalent to "SELECT *, COUNT(1) FROM
....".  The information that the database returns as COUNT(1) will be
available as "$result->get_column('foos')".

Regards,
Jonathan Rockway

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 370 bytes
Desc: OpenPGP digital signature
Url : http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20071028/4f=
a7953e/signature.pgp


More information about the DBIx-Class mailing list