I’m using the onBeforeWrite
method to store some hashed values in my db table. When I publish my page, the values are written to the Product_Live
table as expected, except for the value of a $has_one
relationship (ProductImage). That value is correctly updated in the Product
table, but not Product_Live
. The value saved to Product
is the correct hash value.
I’m assuming the issue is related to the $has_one
relationship, but the solution is just beyond my understanding.
Any assistance in figuring out how to write the value to the _Live
table would be most appreciated.
<?php
use SilverStripe\ORM\DataObject;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Forms\ListboxField;
use SilverStripe\Assets\Image;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Forms\ReadonlyField;
use SilverStripe\Dev\Debug;
class Product extends Page {
private static $db = [
'Price' => 'Decimal',
'Weight' => 'Decimal',
'Measure' => 'Varchar(4)',
'ProdCode' => 'Varchar(25)',
'Type' => 'Enum(array("Sticks","Biscuits","Toppings","Variety"))',
'TitleHash' => 'Varchar(64)',
'PriceHash' => 'Varchar(64)',
'ProdCodeHash' => 'Varchar(64)',
'ProdImgHash' => 'Varchar(64)',
'QuantityHash' => 'Varchar(64)',
'ShipHash' => 'Varchar(64)',
'SubHash' => 'Varchar(64)',
'DefHash' => 'Varchar(64)',
'MonthHash' => 'Varchar(64)'
];
private static $has_one = [
'ProductImage' => Image::class
];
private static $many_many = [
'Recipes' => Recipe::class
];
private static $owns = [
'ProductImage'
];
private static $default_parent = 'ProductHolder';
public function onBeforeWrite() {
parent::onBeforeWrite();
// get api key
$siteConfig = SiteConfig::current_site_config();
$apiKey = $siteConfig->FoxyAPI;
// get image url
if(DataObject::get_by_id(Image::class, $this->ProductImageID)) {
$prodimg = DataObject::get_by_id(Image::class, $this->ProductImageID);
$prodfilename = $prodimg->URL;
} // if URL doesn't exist set an empty value to prevent error
else {
$prodimgurl = '';
}
// get form data
$prodcode = $this->ProdCode;
$price = $this->Price;
$title = $this->Title;
// create hashes
$this->TitleHash = hash_hmac('sha256', $prodcode.'name'.$title, $apiKey);
$this->PriceHash = hash_hmac('sha256', $prodcode.'price'.$price, $apiKey);
$this->ProdCodeHash = hash_hmac('sha256', $prodcode.'code'.$prodcode, $apiKey);
$this->ProdImgHash = hash_hmac('sha256', $prodcode.'image'.$prodfilename, $apiKey);
$this->QuantityHash = hash_hmac('sha256', $prodcode.'quantity--OPEN--', $apiKey);
$this->ShipHash = hash_hmac('sha256', $prodcode.'shipto--OPEN--', $apiKey);
$this->DefHash = hash_hmac('sha256', $prodcode.'category'.'DEFAULT', $apiKey);
$this->SubHash = hash_hmac('sha256', $prodcode.'category'.'SUBSCRIPTIONS', $apiKey);
$this->MonthHash = hash_hmac('sha256', $prodcode.'sub_frequency1m', $apiKey);
}
public function getCMSFields() {
$fields = parent::getCMSFields();
//type
$fields->addFieldToTab('Root.Main', OptionsetField::create('Type', 'Type',
$this->dbObject('Type')->enumValues()), 'Content');
$fields->addFieldToTab('Root.Recipes',
ListboxField::create(
'Recipes',
'Recipes',
Recipe::get()
)
);
// Shopping cart data
$fields->addFieldToTab('Root.CartData', TextField::create('ProdCode', 'Product Code'));
$fields->addFieldToTab('Root.CartData', TextField::create('Price', 'Price $'));
$fields->addFieldToTab('Root.CartData', TextField::create('Weight', 'Weight'));
$fields->addFieldToTab('Root.CartData', OptionsetField::create('Measure', 'Measure',
[
'oz' => 'oz',
'lb' => 'lb',
'each' => 'each'
]
));
$fields->addFieldToTab('Root.CartData', $prodImg = UploadField::create('ProductImage', 'Product Image (1000x750, .jpg, .gif or .png only)'), 'Content');
$prodImg->setFolderName('Product Images')->getValidator()->setAllowedExtensions = ['jpg', 'jpeg', 'gif', 'png'];
return $fields;
}
}