[Catalyst] Re: Template creates objects via belongs_to autovivification

Wade.Stuart at fallon.com Wade.Stuart at fallon.com
Wed Jun 13 17:39:12 GMT 2007







Matt Rosin <mattrosin at yahoo.com> wrote on 06/13/2007 10:44:36 AM:

> Hi, thanks for your comment. But,
>
> 1. I use NOT_NULL so my program has less opportunity to ever crash
> :) no really I saw DBIxC crash in the past I'm pretty sure though
> cannot document now, perhaps the calling a method on a nonexistent
> object error... I would prefer it to say "0" than to crash. Perhaps
> other people have smarter ways to improve robustness...

To again paraphrase Jason, he is saying the way dbic handles belongs_to in
this situation is that it will create a new row if the relationship is not
optional and by setting the FK as not null you are in effect saying that
"this relationship is not optional".  In other words by making the fk
enforce not null you are asking for this behavior.

>
> 2. At any rate, I do not think it is doing the right thing,
> regardless. It is a bug if you cannot test for whether the related
> object exists, and when an SQL insert is generated on another table.

No,  it is enforcing the "not optional" relationship by "doing the right
thing".

> In particular I think this is happening mainly during interpretation
> of TT2's dot notation. I would expect
> $mytransaction->customer->b_company not to create a Customer object
> in the db too. Am I wrong in this?

Yes, you have specified by setting the fk as not null and using belongs_to
that it should do just this.

> So should I either allow NULL in
> transaction.customer or else use might_have? The might_have docs
> suggest cascading updates too...

Your business and application logic should determine the relationships and
how they are enforced.  Should your "transactions" always be linked to a
customer? Should you have a dummy customer that can link to transactions
added to the system in this code path?  Does ...?   Maybe you would get
better advice if you removed the "How" you currently have implemented it
and describe the business/app logic that you want to implement in English.
Something such as:

A user may create a transaction which should be linked to a customer
record.  Users may also create transactions which are not linked to
customer records.  Transactions may be linked to multiple customer records.
Customer records may or may not have any associated transactions.

The more specific your requirements the better the advice you will receive.


-Wade




More information about the Catalyst mailing list