__PACKAGE__->parent_column('parent_id');
Declares the name of the column that contains the self-referential
-ID which defines the parent row. This will create a has_many (children)
+ID which defines the parent row. This will create a has_many (children)
and belongs_to (parent) relationship.
This method also sets up an additional has_many relationship called
sub is_leaf {
my( $self ) = @_;
- my $has_child = $self->result_source->resultset->search(
- { $self->_parent_column => $self->id() },
- { limit => 1 }
- )->count();
+ my $has_child = $self->children_rs->count();
return $has_child ? 0 : 1;
}
1;
__END__
+=head1 CAVEATS
+
+=head2 Generated schemas
+
+If you are using L<DBIx::Class::Schema/deploy> methods either directly
+or via L<DBIx::Class::DeploymentHandler> or L<DBIx::Migration>, you
+will need the following L<SQL::Translator> hook to remove the constraint
+from C<parent_column>, otherwise you will not be able to user 0 (zero) as
+a value.
+
+ sub sqlt_deploy_hook {
+ my ($self, $sqlt_table) = @_;
+
+ foreach my $constraint ($sqlt_table->get_constraints) {
+ if ($constraint->field_names->[0] eq $self->parent_column) {
+ $sqlt_table->drop_constraint($constraint->name);
+ last;
+ }
+ }
+ }
+
+ALso see L<DBIx::Class::Manual::Cookbook/Adding Indexes And Functions To
+Your SQL> for other examples.
+
=head1 INHERITED METHODS
=head2 DBIx::Class