Re: What is the right way to handle a hierarchy of same-class objects?
[prev]
[thread]
[next]
[Date index for 2005/02/22]
On 22 Feb 2005, at 02:25, Karjala wrote:
> However it would be nice to get undef as an answer.
>
> I have the following scenario on my sites:
>
> Class A objects get created and deleted all the time.
>
> Class B objects (a history log) have a field that refers to a class A
> object (which may or may not exist at the present time). Class B
> objects never get deleted.
>
> I need to check with a short piece of code if given a B object, the
> corresponding A object exists, in order to display a link on the
> website if it does exist.
>
> $b->a would be enough, if it would return null instead of an object
> reference.
$b->a will return undef if the 'a' column is NULL. Or it will in mysql
and SQLite, anyway: I can't speak for the grown-up databases. That's
what I meant by 'it should work as you expect'. Sorry: wasn't very
clear.
If the 'a' column contains a defined value - even 0 - then cdbi will
try to inflate it. With recent versions, if you try to inflate from a
broken foreign key you get a fully-formed but empty cdbi object,
because CDBI::Relationship::HasA is lazy (in the good sense) and calls
_init($fkey) without ever touching the foreign table.
I suppose ideally you'd get undef back at that point, but the integrity
check is presumably omitted for good reasons, and I'm keeping quiet
because it's all such a great improvement over the old behaviour, in
which cdbi would _croak if the foreign object didn't exist or the
column was empty...
and as long as your relationship column defaults to NULL everything
works nicely.
best
will