Adding a column to an Object in an Extension

rojanu aliustek at gmail.com
Fri Sep 9 14:45:47 UTC 2011


Hi all,

I have added a column to Bugzilla::Field::Choice in my extension,
update works but creating a new value doesn't create the new column.
When I added warn messages to code it seems that
Bugzilla::Field::Choice::create runs before "object_before_create"
hook.

How can I make sure that group_id field is added to object?

<pre>
sub install_update_db {
    my ($self, $args) = @_;

    my $dbh = Bugzilla->dbh;
    my @standard_fields =
        qw(bug_status resolution priority bug_severity op_sys
rep_platform);
    my $custom_fields = $dbh->selectcol_arrayref(
        'SELECT name FROM fielddefs WHERE custom = 1 AND type
IN(?,?)',
        undef, FIELD_TYPE_SINGLE_SELECT, FIELD_TYPE_MULTI_SELECT);
    foreach my $field_name (@standard_fields, @$custom_fields) {
    	if (!$dbh->bz_column_info($field_name, 'group_id')){
    	    my $field = new Bugzilla::Field({'name' => $field_name});
	        my $group = Bugzilla::Field::ChoiceGroup->create({
		        name   => "Default",
        		field => $field,
		        sortkey => "0",
		    });
        	$dbh->bz_add_column($field_name, 'group_id',
        												{TYPE => 'INT2'},
        												$group->id);
	        $dbh->bz_add_index($field_name, "${field_name}
_group_id_idx",
                           ['group_id']);
		}
    }
}

#############################
#                  Objects
#
#############################
sub object_columns {
    my ($self, $args) = @_;
    my ($class, $columns) = @$args{qw(class columns)};

    if ($class->isa('Bugzilla::Field::Choice')) {
        push(@$columns, 'group_id');
    }
}

sub object_before_create {
    my ($self, $args) = @_;
    my ($class, $params) = @$args{qw(class params)};
    if ($class->isa('Bugzilla::Field::Choice')) {
       warn "Creating CHOICE";
        my $input = Bugzilla->input_params;
        my $group_id = $input->{'group_id'};
        trick_taint($group_id);
        $params->{group_id}   = $group_id;
        warn "GID: ".$params->{group_id};
    }
}

sub object_validators {
    my ($self, $args) = @_;
    my ($class, $validators) = @$args{qw(class validators)};

    if ($class->isa('Bugzilla::Field::Choice')) {
        $validators->{group_id} = \&_check_group_id;
    }
}

sub object_end_of_create {
    my ($self, $args) = @_;

    my $class  = $args->{'class'};
    my $object = $args->{'object'};

	if ($class->isa('Bugzilla::Field')) {
		my $created_group = Bugzilla::Extension::FieldGroups::Group-
>create({
            name   => "Default",
            field => $class,
            sortkey => "0",
    	});
	}
}

sub object_update_columns {
    my ($self, $args) = @_;
    my ($object, $columns) = @$args{qw(object columns)};

    if ($object->isa('Bugzilla::Field::Choice')) {
        push(@$columns, 'group_id');
    }
}

sub object_end_of_set {
    my ($self, $args) = @_;

    my ($object, $field) = @$args{qw(object field)};

    if ($object->isa('Bugzilla::Field::Choice')) {
        my $input = Bugzilla->input_params;
        if ($object->{group_id} != $input->{'group_id'}) {
            my $group_id = $input->{'group_id'};
            trick_taint($group_id);
        	$object->{group_id} = $group_id;
        }
    }
}

sub object_end_of_set_all {
    my ($self, $args) = @_;
    my ($object) = $args->{object};
    warn "FINISHED SET ALL";
    if ($object->isa('Bugzilla::Field::Choice')) {
        my $input = Bugzilla->input_params;
        $object->set('group_id',   $input->{'group_id'});
    }
}
</pre>
_______________________________________________
dev-apps-bugzilla mailing list
dev-apps-bugzilla at lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-apps-bugzilla



More information about the developers mailing list