{"id":263,"date":"2010-03-11T15:20:19","date_gmt":"2010-03-11T15:20:19","guid":{"rendered":"http:\/\/esferas.org\/msqlu\/2010\/03\/11\/apuntes-sobre-dbixclass\/"},"modified":"2010-03-11T15:20:19","modified_gmt":"2010-03-11T15:20:19","slug":"apuntes-sobre-dbixclass","status":"publish","type":"post","link":"https:\/\/esferas.org\/msqlu\/2010\/03\/11\/apuntes-sobre-dbixclass\/","title":{"rendered":"DBIx::Class &#8211; apuntes"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:2019,&quot;href&quot;:&quot;http:\\\/\\\/search.cpan.org\\\/~ribasushi\\\/DBIx-Class-0.08120\\\/lib\\\/DBIx\\\/Class\\\/ResultSource.pm&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20111030120439\\\/http:\\\/\\\/search.cpan.org:80\\\/~ribasushi\\\/DBIx-Class-0.08120\\\/lib\\\/DBIx\\\/Class\\\/ResultSource.pm&quot;,&quot;redirect_href&quot;:&quot;https:\\\/\\\/metacpan.org\\\/pod\\\/release\\\/RIBASUSHI\\\/DBIx-Class-0.08120\\\/lib\\\/DBIx\\\/Class\\\/ResultSource.pm&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:2020,&quot;href&quot;:&quot;http:\\\/\\\/perldoc.perl.org\\\/functions\\\/package.html&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20200712174646\\\/https:\\\/\\\/perldoc.perl.org\\\/functions\\\/package.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-04-16 03:41:16&quot;,&quot;http_code&quot;:503}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-16 03:41:16&quot;,&quot;http_code&quot;:503},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:2021,&quot;href&quot;:&quot;http:\\\/\\\/perldoc.perl.org\\\/functions\\\/qw.html&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20190714201438\\\/https:\\\/\\\/perldoc.perl.org\\\/functions\\\/qw.html&quot;,&quot;redirect_href&quot;:&quot;https:\\\/\\\/perldoc.perl.org\\\/functions\\\/qw.html&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:2022,&quot;href&quot;:&quot;http:\\\/\\\/perldoc.perl.org\\\/functions\\\/q.html&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20200628204012\\\/https:\\\/\\\/perldoc.perl.org\\\/functions\\\/q.html&quot;,&quot;redirect_href&quot;:&quot;https:\\\/\\\/perldoc.perl.org\\\/functions\\\/q.html&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:2023,&quot;href&quot;:&quot;http:\\\/\\\/search.cpan.org\\\/~ribasushi\\\/DBIx-Class-0.08120\\\/lib\\\/DBIx\\\/Class\\\/Schema.pm&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20130611084127\\\/http:\\\/\\\/search.cpan.org:80\\\/~ribasushi\\\/DBIx-Class-0.08120\\\/lib\\\/DBIx\\\/Class\\\/Schema.pm&quot;,&quot;redirect_href&quot;:&quot;https:\\\/\\\/metacpan.org\\\/pod\\\/release\\\/RIBASUSHI\\\/DBIx-Class-0.08120\\\/lib\\\/DBIx\\\/Class\\\/Schema.pm&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:2024,&quot;href&quot;:&quot;http:\\\/\\\/search.cpan.org\\\/~ribasushi\\\/DBIx-Class-0.08120\\\/lib\\\/DBIx\\\/Class\\\/Manual\\\/Intro.pod#Connecting&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;https:\\\/\\\/metacpan.org\\\/pod\\\/release\\\/RIBASUSHI\\\/DBIx-Class-0.08120\\\/lib\\\/DBIx\\\/Class\\\/Manual\\\/Intro.pod&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:2025,&quot;href&quot;:&quot;http:\\\/\\\/search.cpan.org\\\/~ribasushi\\\/DBIx-Class-0.08120\\\/lib\\\/DBIx\\\/Class\\\/ResultSet.pm&quot;,&quot;archived_href&quot;:&quot;https:\\\/\\\/web-wp.archive.org\\\/web\\\/20121006030623\\\/http:\\\/\\\/search.cpan.org:80\\\/~ribasushi\\\/DBIx-Class-0.08120\\\/lib\\\/DBIx\\\/Class\\\/ResultSet.pm&quot;,&quot;redirect_href&quot;:&quot;https:\\\/\\\/metacpan.org\\\/pod\\\/release\\\/RIBASUSHI\\\/DBIx-Class-0.08120\\\/lib\\\/DBIx\\\/Class\\\/ResultSet.pm&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>Peque\u00f1a gu\u00eda de referencia para refrescar el uso de esta herramienta.<\/p>\n<p><!--more--><\/p>\n<h4>Definici\u00f3n de datos<\/h4>\n<p>La definici\u00f3n de datos ser hace en dos partes: se crean paquetes Perl para cada tabla (&#8216;resultset&#8217;)<br \/>\ny un paquete que los engloba en un esquema base (&#8216;schema&#8217;).<\/p>\n<h3>Definir un tabla (resultsource)<\/h3>\n<p>Documentaci\u00f3n de referencia en<br \/>\n<a href=\"http:\/\/search.cpan.org\/~ribasushi\/DBIx-Class-0.08120\/lib\/DBIx\/Class\/ResultSource.pm\">DBIx::Class::ResultSource<\/a>.<\/p>\n<pre><ol><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><a href=\"http:\/\/perldoc.perl.org\/functions\/package.html\"><span style=\"color: #000066\">package<\/span><\/a> Mi::<span style=\"color: #006600\">Esquema<\/span>::<span style=\"color: #006600\">MiTabla<\/span>;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #000000;font-weight: bold\">use<\/span> base <a href=\"http:\/\/perldoc.perl.org\/functions\/qw.html\"><span style=\"color: #000066\">qw<\/span><\/a><span style=\"color: #66cc66\">&#040;<\/span>DBIx::<span style=\"color: #006600\">Class<\/span><span style=\"color: #66cc66\">&#041;<\/span>;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">&nbsp;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">__PACKAGE__-<span style=\"color: #66cc66\">&gt;<\/span><span style=\"color: #006600\">load_components<\/span><span style=\"color: #66cc66\">&#040;<\/span> <a href=\"http:\/\/perldoc.perl.org\/functions\/qw.html\"><span style=\"color: #000066\">qw<\/span><\/a><span style=\"color: #66cc66\">&#040;<\/span> Core <span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #66cc66\">&#041;<\/span>;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">&nbsp;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">__PACKAGE__-<span style=\"color: #66cc66\">&gt;<\/span><span style=\"color: #006600\">table<\/span><span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #ff0000\">'tabla'<\/span><span style=\"color: #66cc66\">&#041;<\/span>;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">__PACKAGE__-<span style=\"color: #66cc66\">&gt;<\/span><span style=\"color: #006600\">add_columns<\/span><span style=\"color: #66cc66\">&#040;<\/span> <a href=\"http:\/\/perldoc.perl.org\/functions\/qw.html\"><span style=\"color: #000066\">qw<\/span><\/a><span style=\"color: #66cc66\">&#040;<\/span> codigo descripcion <span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #66cc66\">&#041;<\/span>;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">__PACKAGE__-<span style=\"color: #66cc66\">&gt;<\/span><span style=\"color: #006600\">primary_key<\/span><span style=\"color: #66cc66\">&#040;<\/span> <a href=\"http:\/\/perldoc.perl.org\/functions\/q.html\"><span style=\"color: #000066\">q<\/span><\/a><span style=\"color: #66cc66\">&#040;<\/span>codigo<span style=\"color: #66cc66\">&#041;<\/span> <span style=\"color: #66cc66\">&#041;<\/span>;<\/div><\/li><\/ol><\/pre>\n<h3>Crear un esquema de base de datos (schema)<\/h3>\n<p>Documentaci\u00f3n de referencia en<br \/>\n<a href=\"http:\/\/search.cpan.org\/~ribasushi\/DBIx-Class-0.08120\/lib\/DBIx\/Class\/Schema.pm\">DBIx::Class::Schema<\/a>.<\/p>\n<pre><ol><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><a href=\"http:\/\/perldoc.perl.org\/functions\/package.html\"><span style=\"color: #000066\">package<\/span><\/a> Mi::<span style=\"color: #006600\">Esquema<\/span>;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #000000;font-weight: bold\">use<\/span> base <a href=\"http:\/\/perldoc.perl.org\/functions\/qw.html\"><span style=\"color: #000066\">qw<\/span><\/a><span style=\"color: #66cc66\">&#040;<\/span>DBIx::<span style=\"color: #006600\">Class<\/span>::<span style=\"color: #006600\">Schema<\/span><span style=\"color: #66cc66\">&#041;<\/span>;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">&nbsp;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #808080;font-style: italic\"># Cargar\u00e1 todas las definiciones de tablas ('resultset') <\/span><\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #808080;font-style: italic\"># bajo Mi::Esquema::*<\/span><\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">__PACKAGE__-<span style=\"color: #66cc66\">&gt;<\/span><span style=\"color: #006600\">load_components<\/span><span style=\"color: #66cc66\">&#040;<\/span> <span style=\"color: #66cc66\">&#041;<\/span>;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">&nbsp;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #cc66cc\">1<\/span>;<\/div><\/li><\/ol><\/pre>\n<h4>Acceso a los datos<\/h4>\n<p>El acceso a los datos consiste en <em>leer<\/em> (buscar y recuperar registros), y<br \/>\n\u00aba\u00f1adir\u00bb, \u00abactualizar\u00bb y \u00abeliminar\u00bb registros, previa conexi\u00f3n con la<br \/>\nfuente de datos.<\/p>\n<h3>Conectar con la fuente de datos<\/h3>\n<p>Documentaci\u00f3n de referencia en <a href=\"http:\/\/search.cpan.org\/~ribasushi\/DBIx-Class-0.08120\/lib\/DBIx\/Class\/Manual\/Intro.pod#Connecting\">Connecting<\/a>.<\/p>\n<pre><ol><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #000000;font-weight: bold\">use<\/span> Mi::<span style=\"color: #006600\">Esquema<\/span>;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">&nbsp;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #b1b100\">my<\/span> <span style=\"color: #0000ff\">$esquema<\/span> = Mi::<span style=\"color: #006600\">Esquema<\/span>-<span style=\"color: #66cc66\">&gt;<\/span><span style=\"color: #006600\">connect<\/span><span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #ff0000\">'dbi...'<\/span><span style=\"color: #66cc66\">&#041;<\/span>;<\/div><\/li><\/ol><\/pre>\n<h3>Buscar y\/\u00f3 leer datos<\/h3>\n<p>Documentaci\u00f3n de referencia en<br \/>\n<a href=\"http:\/\/search.cpan.org\/~ribasushi\/DBIx-Class-0.08120\/lib\/DBIx\/Class\/ResultSet.pm\">DBIx::Class::ResultSet<\/a>.<\/p>\n<pre><ol><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #808080;font-style: italic\"># Primero obtenemos un objeto de tipo 'resultset'<\/span><\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #808080;font-style: italic\"># que viene a ser similar a 'prepare' ...  <\/span><\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #b1b100\">my<\/span> <span style=\"color: #0000ff\">$result_set<\/span> = <span style=\"color: #0000ff\">$esquema<\/span>-<span style=\"color: #66cc66\">&gt;<\/span><span style=\"color: #006600\">resulset<\/span><span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #ff0000\">'Tabla'<\/span><span style=\"color: #66cc66\">&#041;<\/span>;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">&nbsp;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #808080;font-style: italic\"># y luego, sobre ese 'resultset' llamamos a m\u00e9todos concretos, <\/span><\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #808080;font-style: italic\"># bien buscando por la clave primaria como en 'find' y obteniendo una<\/span><\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #808080;font-style: italic\"># instancia del objeto 'Mi::Esquema::Tabla' <\/span><\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #b1b100\">my<\/span> <span style=\"color: #0000ff\">$record<\/span> = <span style=\"color: #0000ff\">$result_set<\/span>-<span style=\"color: #66cc66\">&gt;<\/span><span style=\"color: #006600\">find<\/span><span style=\"color: #66cc66\">&#040;<\/span> <span style=\"color: #ff0000\">'00010'<\/span> <span style=\"color: #66cc66\">&#041;<\/span>;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\">&nbsp;<\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #808080;font-style: italic\"># \u00f3 empleando expresiones m\u00e1s complejas para conseguir una <\/span><\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #808080;font-style: italic\"># instancia de 'DBIx::Class::ResultSet' como con 'search'<\/span><\/div><\/li><li style=\"font-family: 'Courier New', Courier, monospace;color: black;font-weight: normal;font-style: normal\"><div style=\"font-family: 'Courier New', Courier, monospace;font-weight: normal\"><span style=\"color: #b1b100\">my<\/span> <span style=\"color: #0000ff\">$result_set<\/span> = <span style=\"color: #0000ff\">$result_set<\/span>-<span style=\"color: #66cc66\">&gt;<\/span><span style=\"color: #006600\">search<\/span><span style=\"color: #66cc66\">&#040;<\/span><span style=\"color: #66cc66\">&#041;<\/span>;<\/div><\/li><\/ol><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Peque\u00f1a gu\u00eda de referencia para refrescar el uso de esta herramienta.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"footnotes":""},"categories":[2],"tags":[79,17],"class_list":["post-263","post","type-post","status-publish","format-standard","hentry","category-software","tag-bases-de-datos","tag-perl"],"_links":{"self":[{"href":"https:\/\/esferas.org\/msqlu\/wp-json\/wp\/v2\/posts\/263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/esferas.org\/msqlu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/esferas.org\/msqlu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/esferas.org\/msqlu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/esferas.org\/msqlu\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":0,"href":"https:\/\/esferas.org\/msqlu\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"wp:attachment":[{"href":"https:\/\/esferas.org\/msqlu\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/esferas.org\/msqlu\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/esferas.org\/msqlu\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}