Interchange6-Schema

 view release on metacpan or  search on metacpan

lib/Interchange6/Schema/Base/Attribute.pm  view on Meta::CPAN

relationships like L<Interchange6::Schema::Result::User>,
L<Interchange6::Schema::Result::Navigation>
and L<Interchange6::Schema::Result::Product>.

=over 4

=item B<Assumptions>

This module assumes that your using standardized class naming.

example: User in this example is the $base class so UserAttribute, 
UserAttributeValue class naming would be used.  These would
also use user_attributes_id and user_attributes_values_id as primary
keys.  In general follow the example classes listed in description.

=back

=cut

=head1 SYNOPSIS

    $navigation_object->add_attribute('meta_title','My very seductive title here!');

=head1 METHODS

=head2 add_attribute

Add attribute.

    $base->add_attribute('hair_color', 'blond');

Where 'hair_color' is Attribute and 'blond' is AttributeValue

=cut

sub add_attribute {
    my ($self, $attr, $attr_value) = @_;
    my $base = $self->result_source->source_name;

    # find or create attributes
    my ($attribute, $attribute_value) = $self->find_or_create_attribute($attr, $attr_value);

    # create base_attribute object
    my $base_attribute = $self->find_or_create_related(lc($base) . '_attributes',
                                                       {attributes_id => $attribute->id});
    # create base_attribute_value
    $base_attribute->create_related(lc($base) . '_attribute_values',
                                    {attribute_values_id => $attribute_value->id});

    return $self;
}

=head2 update_attribute_value

Update base attribute value

    $base->update_attribute('hair_color', 'brown');

=cut

sub update_attribute_value {
    my ($self, $attr, $attr_value) = @_;
    my $base = $self->result_source->source_name;

    my ($attribute, $attribute_value) = $self->find_or_create_attribute($attr, $attr_value);

    my (undef, $base_attribute_value) = $self->find_base_attribute_value($attribute, $base);

    $base_attribute_value->update({attribute_values_id => $attribute_value->id});

    return $self;
}

=head2 delete_attribute

Delete $base attribute

    $base->delete_attribute('hair_color', 'purple');

=cut

sub delete_attribute {
    my ($self, $attr, $attr_value) = @_;
    my $base = $self->result_source->source_name;

    my ($attribute) = $self->find_or_create_attribute($attr, $attr_value);

    my ($base_attribute, $base_attribute_value) = $self->find_base_attribute_value($attribute, $base);

    #delete
    $base_attribute_value->delete;
    $base_attribute->delete;

    return $self;
}

=head2 search_attributes

Returns attributes resultset for a $base object

    $rs = $base->search_attributes;

You can pass conditions and attributes to the search like for
any L<DBIx::Class::ResultSet>, e.g.:

    $rs = $base->search_attributes(
        undef, { order_by => 'priority desc' });

=cut

sub search_attributes {
    my ($self, $condition, $search_atts) = @_;

    my $base = $self->result_source->source_name;

    my $base_attributes = $self->search_related(lc($base) . '_attributes');

    my $attributes = $base_attributes->search_related('attribute',
                                                  $condition, $search_atts);

    return $attributes;



( run in 1.118 second using v1.01-cache-2.11-cpan-bbe5e583499 )