'Attach a component
@param alias: the alias
@param hook: the hook'

def _attach(self, alias, hook):

    filterby = hook.filterby
    if ((alias is not None) and (filterby is not None)):
        table_alias = ('%s_%s_%s' % (hook.prefix, hook.alias, hook.name))
        table = hook.table.with_alias(table_alias)
        table._id = table[table._id.name]
        hook.table = table
    else:
        table_alias = None
        table = hook.table
    component = S3Resource(table, parent=self, alias=alias, linktable=hook.linktable, include_deleted=self.include_deleted, approved=self._approved, unapproved=self._unapproved)
    if table_alias:
        component.tablename = hook.tablename
        component._alias = table_alias
    component.pkey = hook.pkey
    component.fkey = hook.fkey
    component.linktable = hook.linktable
    component.lkey = hook.lkey
    component.rkey = hook.rkey
    component.actuate = hook.actuate
    component.autodelete = hook.autodelete
    component.autocomplete = hook.autocomplete
    component.multiple = hook.multiple
    component.defaults = hook.defaults
    if (not filterby):
        component.filter = None
    else:
        query = None
        for (k, v) in filterby.items():
            if isinstance(v, FS):
                try:
                    rfield = v.resolve(self)
                except (AttributeError, SyntaxError):
                    if current.response.s3.debug:
                        raise
                    else:
                        current.log.error(sys.exc_info()[1])
                        continue
                field = rfield.field
                if ((not field) or (field.table != self.table)):
                    current.log.error(("Component    filter    for    %s<=%s:    invalid    lookup    field    '%s'" % (self.tablename, alias, v.name)))
                    continue
                subquery = (table[k] == field)
            else:
                is_list = isinstance(v, (tuple, list))
                if (is_list and (len(v) == 1)):
                    filterfor = v[0]
                    is_list = False
                else:
                    filterfor = v
                if (not is_list):
                    subquery = (table[k] == filterfor)
                elif filterfor:
                    subquery = table[k].belongs(set(filterfor))
                else:
                    continue
            if subquery:
                if (query is None):
                    query = subquery
                else:
                    query &= subquery
        component.filter = query
    link = component.link
    if (link is not None):
        link.pkey = component.pkey
        link.fkey = component.lkey
        link.multiple = component.multiple
        link.actuate = component.actuate
        link.autodelete = component.autodelete
        links = self.links
        links[link.name] = links[link.alias] = link
    self.components[alias] = component
