[Dbix-class] Re: Template creates objects via belongs_to (how to lay out relationships)

Matt S Trout dbix-class at trout.me.uk
Sat Jun 16 17:20:53 GMT 2007


On Fri, Jun 15, 2007 at 10:58:35AM -0500, Brian Kirkbride wrote:
> Matt Rosin wrote:
> >P.S. I should add that TT2 does not seem to let you test existence of a 
> >related object, it just autovivifies. The .defined command and other 
> >things I tried were not usable. However in my Perl code I am able to do 
> >such tests (i.e. get_column, or check to see if customer is defined).
> >
> > 
> >
> >So I am not sure if the autovivification is only something from TT2 or 
> >if it happens in the Perl code too (this is in a Catalyst environment 
> >with MySQL). Thanks.
> >
> > 
> >
> >Matt
> 
> No, this is not limited to TT2, it is (I believe) the intended 
> behaviour of a belongs_to relationship.  I can verify that something like:
> 
> My::Foo->belongs_to(bar => 'My::Bar', 'bar');
> 
> $foo = My::Foo->find(1);
> $foo->update({bar => 1234}); # doesn't exist
> unless ($foo->bar) {
> 	die("Not found");
> }
> 
> will not die, but rather create a Bar row with pk 1234.  This appears 
> to be by design, as belongs_to creates an accessor of type "filter" 
> which will call find_or_create() instead of find() on the related 
> table (Bar in this case).
> 
> I think this only happens when the rel accessor is shared with the 
> column accessor for the join.  Meaning that a rel named "bar" on 
> "bar_id" would not have this problem, because the accessor type would 
> be "single" instead of "filter".

Ideally, the 'filter' approach would vanish entirely - it's really a
legacy thing that came through from CDBI-style has_a rel design.

At this stage it's likely to happen in the next rework of the accessor
system, which will probably happen for 09.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/             http://www.shadowcatsystems.co.uk/ 



More information about the Dbix-class mailing list