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

John Goulah jgoulah at gmail.com
Mon Oct 29 04:16:47 GMT 2007


On 10/28/07, Randy Moore <ramoore at axion-it.com> wrote:
>
>  Jonathan Rockway wrote:
>
> 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
>
>  Ok, I now see that the 'AS' info does not actually get built into the SQL
> query.  So, what I'm trying to do will not work.
>


Also, from the FAQ
http://search.cpan.org/~ash/DBIx-Class-0.08007/lib/DBIx/Class/Manual/FAQ.pod

.. sort my results based on fields I've aliased using as?

You don't. You'll need to supply the same functions/expressions to order_by,
as you did to select.

To get "fieldname AS alias" in your SQL, you'll need to supply a literal
chunk of SQL in your select attribute, such as:

 ->search({}, { select =3D> [ \'now() AS currenttime'] })

Then you can use the alias in your order_by attribute.




hth,
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20071029/8e3=
a1dd9/attachment.htm


More information about the DBIx-Class mailing list