DBIx::Class + Catalyst::View::JSON
InflateColumnã使ã£ã¦ããã¨ãã«åé¡ããã£ããããä¿®æ£çãæ°ããã¨ã³ããªã«èµ·ããã¾ããã
ä¿®æ£çï¼DBIx::Class + Catalyst::View::JSON(2) - ヒルズで働く@robarioの技ログ
ææ¸ããã¡ã¢ãåºã¦ããã®ã§è»¢è¼ã
DBIC::ResultSet#findãDBIC::ResultSet#searchãããã®ããã®ã¾ã¾JSONã«ããããããªãã§ããã
ã§ãCatalyst::View::JSONã«ãã®ã¾ã¾æ¸¡ãã¨Catalystãè½ã¡ããã§ããã
ãªã®ã§é©å½ã«å±éãã¦ããããµãã«ã¼ãã³ãä½ã£ã¦ä½¿ã£ã¦ãã¾ãã
Catalyst::View::JSON以å¤ã§ã使ãã®ã§ãã¢ããªã±ã¼ã·ã§ã³ã¯ã©ã¹ã«ç½®ãã¦ãã¾ãã
### lib/MyApp.pm sub expand_dbic { my ( $c, $obj ) = @_; if ( !Scalar::Util::blessed($obj) ) { return $obj; } if ( $obj->isa('DBIx::Class::Row') ) { return { $obj->get_columns }; } if ( $obj->isa('DBIx::Class::ResultSet') ) { return [ map { +{ $_->get_columns } } $obj->all ]; } return $obj; } ### lib/MyApp/View/JSON.pm package MyApp::View::JSON; use base qw(Catalyst::View::JSON); sub process { my ( $self, $c ) = @_; $c->stash->{response} = $c->expand_dbic( $c->stash->{response} || {} ); $self->SUPER::process($c); }
使ç¨ä¾
### /lib/MyApp/Controller/Root.pm sub default : Path { my ( $self, $c ) = @_; $c->stash->{response} = $c->model('DBIC')->resultset('Example')->find(...); } sub index : Private { my ( $self, $c ) = @_; $c->stash->{response} = $c->model('DBIC')->resultset('Example')->search(...); } sub end : Private { my ( $self, $c ) = @_; $c->forward($c->view('JSON')); }