Hi,<br>
<br>
My friend solved the problem. It's weird though and I am still a little bit confused. The way I am trying to access/display the rows is like this:<br>
<br>
[% FOREACH railway = railways -%]<br>
&nbsp; [% railway.railway_station.railway_station_fields %]<br>
[% END -%]<br>
<br>
And as Nigel pointed out,<br>
<pre><font size="3"><tt><tt>&gt; Single object which returns an array from the has_many accessor.</tt></tt></font></pre>If I use 'join' only, I could get all the data for railways which only have 1 row of railway_station by using "railway.railway_station", but for railways which have more than 1
 rows of railway_station, I had to use something like "railway.railway_station.0". But it always failed for "prefetch"<br>
<br>
The way I wanted it, is that, since my search code is like this,<br>
<br>
my $railways = $schema-&gt;resultset('Railway')-&gt;search(<br>
{<br>
&nbsp;'me.railway_id' =&gt; { 'IN' =&gt; $selected_railway_ids },<br>
&nbsp;'railway_station.main_flag' =&gt; 1, ## forgot to mention this earlier<br>
},<br>
{<br>
&nbsp; join =&gt; 'railway_station',<br>
&nbsp; prefetch =&gt; 'railway_station',<br>
&nbsp; }<br>
);<br>
$c-&gt;stash-&gt;{railways} = [$railways-&gt;all];<br>
<br>
<br>
And although railway has 'has_many' rel, with the condition main_flag=1, only 1 or null railway_station should be returned for each railway, and therefore, I am hoping to access it by&nbsp; [% railway.railway_station%]<br>
<br>
The way that my friend solved it, was to declare a select variable<br>
<br>
&nbsp;&nbsp;&nbsp; my @select = (<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qw/&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  &nbsp;&nbsp;  <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; railway_field_1<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; railway_field_2<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; main_flag&nbsp;&nbsp; #from railway_station<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; railway_station_field1 #from railway_station<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /<br>
&nbsp;&nbsp;&nbsp; );<br>
<br>
and put it in the search condition like this:<br>
<br>
my $railways = $schema-&gt;resultset('Railway')-&gt;search(<br>
 {<br>
 &nbsp;'me.railway_id' =&gt; { 'IN' =&gt; $selected_railway_ids },<br>
 &nbsp;'railway_station.main_flag' =&gt; 1, ## forgot to mention this earlier<br>
 },<br>
 {<br>
&nbsp; select =&gt; \@select,<br>
&nbsp; join =&gt; 'railway_station',<br>
 }<br>
 );<br>
<br>
and he wasn't using prefetch at all. Now, I could access 'main flag' field by using [% railway.main_flag.%], instead of <br>
[% railway.railway_station.main_flag %]<br>
Any ideas why this happened?<br>
<br>
Sindharta<br>
 <br>
<br>
<b>Jess Robinson &lt;castaway@desert-island.me.uk&gt;</b> wrote:<blockquote class="replbq" style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"> <br>
On Tue, 26 Feb 2008, sindharta_tanuwijaya@yahoo.co.jp wrote:<br>
<br>
&gt; Hi,<br>
&gt;<br>
&gt; I am having a problem, in which I couldn't get the relationship column data by using prefetch.<br>
&gt; My relationship is declared like this, suppose we have two tables: railway and railway_station, which relationship is 1 to many.<br>
&gt;<br>
&gt; #in railway schema<br>
&gt; __PACKAGE__-&gt;has_many(<br>
&gt;    "railway_station",<br>
&gt;    "Schema::RailwayStation",<br>
&gt;    { "foreign.railway_station_railway_id" =&gt; "self.railway_id" },<br>
&gt; );<br>
&gt;<br>
&gt; #in railway_station schema<br>
&gt; __PACKAGE__-&gt;belongs_to(<br>
&gt;  "railway",<br>
&gt;  "Schema::Railway",<br>
&gt;  { "foreign.railway_id" =&gt; "self.railway_station_railway_id" },<br>
&gt; );<br>
&gt;<br>
&gt; My prefetch command is as follows:<br>
&gt;<br>
&gt;   my $railways = $schema-&gt;resultset('Railway')-&gt;search(<br>
&gt;        {<br>
&gt;           'me.railway_id' =&gt; { 'IN' =&gt; $selected_railway_ids },<br>
&gt;        },<br>
&gt;        {<br>
&gt;            join =&gt; 'railway_station',<br>
&gt;            prefetch =&gt; 'railway_station',<br>
&gt;        }<br>
&gt;    );<br>
&gt;<br>
&gt; I could get the values of the columns of the railways correctly, but not the columns of railway_stations.<br>
<br>
You don't tell us *how* you are trying to get the columns of the railway <br>
stations, so I really cant tell you where you have gone wrong. Your search <br>
code is correct.<br>
<br>
You can check what SQL it actually runs and try it for yourself, by <br>
setting DBIC_TRACE=1 in your test script environment.<br>
<br>
&gt; If I use 'join' only though, I could get all the data for railways which only have 1 row of railway_station, but not for<br>
&gt; railways which have more than 1 rows of railway_station.<br>
&gt; Any ideas why this happened ?<br>
<br>
Sounds like you are accessing the data incorrectly.<br>
<br>
&gt; I have a hunch that maybe it's the ID of the railway_station that is causing this problem. In our current database, the primary key of<br>
&gt; railway_station works as a dummy field, in which there are a lot of null values (although they are primary keys).<br>
&gt; Of course, I have my own objections to this current implementation, but I am wondering if that is indeed the problem, since the primary key of railway_station isn't supposed to be used in any query generated above ?<br>
<br>
Well, it wont fetch the ones with null relation values, obviously.. but <br>
would you expect it to?<br>
<br>
&gt; Or maybe I am missing something else here.<br>
<br>
Like giving us your code? ;)<br>
<br>
Jess<br>
<br>
_______________________________________________<br>
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class<br>
IRC: irc.perl.org#dbix-class<br>
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/<br>
Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.rawmode.org<br>
</blockquote><br>

<div style="line-height: 0; width: 0; height: 5px; clear: both;">&nbsp;</div>
<p>&#32;



<hr size=1><a href=http://pr.mail.yahoo.co.jp/toolbar/ target="new">Easy + Joy + Powerful = Yahoo! Bookmarks x Toolbar</a><br>