What is the right way to handle a hierarchy of same-class objects?
[prev]
[thread]
[next]
[Date index for 2005/02/21]
I have a class called (MyTest::)Category.
The table of Category has a field ("father") containing the id of the
father category of each category.
Objects that don't have a father, have 0 in that field.
The problem is that if I check for the truth value of $obj->father, I
get a true value regardless of whether that record has a father or not.
e.g.> if ($obj->father) { print "has father" }
The reason for this is that $obj->father returns an object reference (a
reference of a non-existant object nevertheless).
My class definition is as follows, and my executable is run as a
stand-alone program:
package MyTest::Category;
use base 'MyTest::DBI';
__PACKAGE__->set_up_table('tblCategories');
__PACKAGE__->has_a(father => 'MyTest::Category');
Yet, when I call "retrieve" with a non-existant key, I get null instead
of an object reference.
My questions:
1) Is this how CDBI is supposed to work or is there a contradiction
between "retrieve" and "father"?
2) Should I maybe be using might_have instead of has_a for this case?
Thanks a lot and sorry for bothering you once again,
- Karjala
|
What is the right way to handle a hierarchy of same-class objects?
Karjala 13:12 on 21 Feb 2005
|