public function setAttribute($ak, $value)
    {
        $orm = \Database::connection()->getEntityManager();

        $this->clearAttribute($ak);

        // Create the attribute category value.
        $attributeValue = $this->getAttributeValueObject($ak, true);
        $orm->persist($attributeValue);
        $orm->flush();

        // Create the generic value. This gets joined to any specific Attribute value objects later on.
        $genericValue = new Value();
        $genericValue->setAttributeKey($attributeValue->getAttributeKey());
        $orm->persist($genericValue);
        $orm->flush();

        // Set the generic value to the attribute category value.
        $attributeValue->setGenericValue($genericValue);
        $orm->persist($attributeValue);
        $orm->flush();

        $controller = $attributeValue->getAttributeKey()->getController();
        $controller->setAttributeValue($attributeValue);

        if (!($value instanceof AttributeValue\AbstractValue)) {
            if ($value instanceof EmptyRequestAttributeValue) {
                // LEGACY SUPPORT
                // If the passed $value object == EmptyRequestAttributeValue, we know we are dealing
                // with a legacy attribute type that's not using Doctrine. We have not returned an 
                // attribute value value object.
                $controller->saveForm($controller->post());
                unset($value);
            } else {
                /**
                 * @var $value AttributeValue\AbstractValue
                 */
                $value = $controller->createAttributeValue($value);
            }
        }

        if ($value) {
            $value->setGenericValue($genericValue);
            $orm->persist($value);
            $orm->flush();
        }

        $category = $this->getObjectAttributeCategory();
        $indexer = $category->getSearchIndexer();
        if ($indexer) {
            $indexer->indexEntry($category, $attributeValue, $this);
        }

        return $attributeValue;
    }