[Dbix-class] I get only one in a one to one

neil.lunn neil at mylunn.id.au
Mon Jan 9 23:49:24 GMT 2012


On 10/01/2012 8:09 AM, Miguel Barco wrote:
> But the problem remains the same, because I want to retrieve some =

> columns from the main table an some from the related one, so I was =

> told to use Prefetch.
Which is fine as this is basically an optimisation to avoid going back =

to the database engine to call rows from the related table entry.
>
> What I really need is something as simple as this query (that I can =

> use everywhere):
>
> *SELECT* books.title, contents.content_en
> *FROM* books, contents
> *WHERE* books.id =3D 1111
> *AND* contents.id =3D 1111

Which is more or less exactly what prefetch is doing for you. Set =

DBIC_TRACE=3D1 on the command line before starting you app to see what is =

happening.

But what you are basically missing is that your "contents" table is the =

related table. Therefore for each "book" there are *many* "contents". So =

not only are you iterating through the books, you need to iterate =

through the "contents" as well otherwise of course you are just =

accessing the first line. To see the others you call the next one, and =

so on.

So basically you need to stop thinking in terms of the raw sql results =

and start thinking in terms of the objects that are represented. A =

"book" with many "contents" Which is what an ORM is for.

Neil

>
> I am be very surprised that I am stuck with it!!
>
> Regards:
> Migue
>
>
> ------------------------------------------------------------------------
> *De:* Jorge Gonzalez <jorge.gonzalez at daikon.es>
> *Para:* dbix-class at lists.scsys.co.uk
> *Enviado:* lunes 9 de enero de 2012 10:49
> *Asunto:* Re: [Dbix-class] I get only one in a one to one
>
> You have commented out the line where you get content_en from the =

> CONTENTS table, but you have done so _in HTML_. The template engine is =

> running your template, including your HTML comment, and is getting the =

> content_en field, just to put it in an HTML comment.
>
> If you want to avoid the TT engine calling the relationship you must =

> comment the call _inside_ the TT code, i.e.:
>
> (...)
> [% FOREACH book IN books -%]
> <tr>
> <td>[% book.title %]</td>
> *<td>[% # book.the_content.content_en %]</td>
> *
> </tr>
> [% END -%]
> (...)
>
> And something more: if you just want a column from a relation, do a =

> join, not a prefetch:
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.=
co.uk

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20120110/7c2=
da8ab/attachment-0001.htm


More information about the DBIx-Class mailing list