public function onBeforeWrite()
    {
        parent::onBeforeWrite();

        if (isset($_POST['ProductAttributes']) && is_array($_POST['ProductAttributes'])) {
            $this->AttributeValues()->setByIDList(array_values($_POST['ProductAttributes']));
        }

        $img = $this->Image();

        if ($img && $img->exists()) {
            $img->doPublish();
        }
    }