start of variant values
This commit is contained in:
@@ -50,7 +50,12 @@ class ProductSkusController extends AppController
|
||||
*/
|
||||
public function view($id = null)
|
||||
{
|
||||
$productSku = $this->ProductSkus->get($id, contain: ['Products']);
|
||||
$productSku = $this->ProductSkus->get($id, contain: [
|
||||
'Products',
|
||||
'ProductSkuVariantValues',
|
||||
'ProductSkuVariantValues.ProductCategoryVariants',
|
||||
'ProductSkuVariantValues.ProductCategoryVariantOptions',
|
||||
]);
|
||||
$this->set(compact('productSku'));
|
||||
}
|
||||
|
||||
@@ -91,30 +96,43 @@ class ProductSkusController extends AppController
|
||||
if ($this->request->is('post')) {
|
||||
$postedSkus = $this->request->getData();
|
||||
$saveOptions = [
|
||||
'associated' => [],
|
||||
'associated' => [
|
||||
'ProductSkuVariantValues',
|
||||
],
|
||||
];
|
||||
|
||||
$finalPostData = [];
|
||||
$postedSkus = Hash::insert($postedSkus, '{n}.product_id', $productId);
|
||||
|
||||
foreach ($postedSkus as $postedSkuCnt => $postedSku) {
|
||||
if ($postedSku['add'] ?? false) {
|
||||
if (!isset($postedSku['add']) || !$postedSku['add']) {
|
||||
unset($productSkus[$postedSkuCnt]);
|
||||
|
||||
continue;
|
||||
}
|
||||
unset($postedSkus[$postedSkuCnt]);
|
||||
if ($productSkus[$postedSkuCnt] ?? false) {
|
||||
unset($productSkus[$postedSkuCnt]);
|
||||
}
|
||||
$finalPostData[$postedSkuCnt] = $postedSku;
|
||||
}
|
||||
|
||||
if (!$productSkus || !$postedSkus) {
|
||||
$this->Flash->error('Nothing to save! Add at least one SKU next time.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$productSkus = $table->patchEntities($productSkus, $postedSkus, $saveOptions);
|
||||
if ($table->saveManyOrFail($productSkus, $saveOptions)) {
|
||||
$this->Flash->success(__(count($productSkus) . ' New SKUs have been saved.'));
|
||||
|
||||
$productSkus = $table->patchEntities($productSkus, $finalPostData, $saveOptions);
|
||||
$errors = [];
|
||||
$successes = [];
|
||||
foreach ($productSkus as $productSkuToSave) {
|
||||
if (!$table->save($productSkuToSave, $saveOptions)) {
|
||||
Log::debug(print_r('$productSkuToSave->getErrors()', true));
|
||||
Log::debug(print_r($productSkuToSave->getErrors(), true));
|
||||
|
||||
continue;
|
||||
}
|
||||
$successes[] = $productSkuToSave;
|
||||
}
|
||||
|
||||
if ($successes) {
|
||||
$this->Flash->success(__(count($successes) . ' New SKUs have been saved.'));
|
||||
|
||||
return $this->redirect(['action' => 'index']);
|
||||
}
|
||||
|
||||
39
src/Model/Entity/ProductSkuVariantValue.php
Normal file
39
src/Model/Entity/ProductSkuVariantValue.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeProducts\Model\Entity;
|
||||
|
||||
use Cake\ORM\Entity;
|
||||
|
||||
/**
|
||||
* ProductSkuVariantValue Entity
|
||||
*
|
||||
* @property string $id
|
||||
* @property string $product_sku_id
|
||||
* @property string $product_category_variant_id
|
||||
* @property string $product_category_variant_option_id
|
||||
*
|
||||
* @property ProductSku $product_sku
|
||||
* @property ProductCategoryVariant $product_category_variant
|
||||
* @property ProductCategoryVariantOption $product_category_variant_option
|
||||
*/
|
||||
class ProductSkuVariantValue extends Entity
|
||||
{
|
||||
/**
|
||||
* Fields that can be mass assigned using newEntity() or patchEntity().
|
||||
*
|
||||
* Note that when '*' is set to true, this allows all unspecified fields to
|
||||
* be mass assigned. For security purposes, it is advised to set '*' to false
|
||||
* (or remove it), and explicitly make individual fields accessible as needed.
|
||||
*
|
||||
* @var array<string, bool>
|
||||
*/
|
||||
protected array $_accessible = [
|
||||
'product_sku_id' => true,
|
||||
'product_category_variant_id' => true,
|
||||
'product_category_variant_option_id' => true,
|
||||
'product_skus' => true,
|
||||
'product_category_variant' => true,
|
||||
'product_category_variant_option' => true,
|
||||
];
|
||||
}
|
||||
109
src/Model/Table/ProductSkuVariantValuesTable.php
Normal file
109
src/Model/Table/ProductSkuVariantValuesTable.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeProducts\Model\Table;
|
||||
|
||||
use Cake\Datasource\EntityInterface;
|
||||
use Cake\Datasource\ResultSetInterface;
|
||||
use Cake\ORM\Association\BelongsTo;
|
||||
use Cake\ORM\Query\SelectQuery;
|
||||
use Cake\ORM\RulesChecker;
|
||||
use Cake\ORM\Table;
|
||||
use Cake\Validation\Validator;
|
||||
use CakeProducts\Model\Entity\ProductSkuVariantValue;
|
||||
use Closure;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
|
||||
/**
|
||||
* ProductSkuVariantValues Model
|
||||
*
|
||||
* @property ProductSkusTable&BelongsTo $ProductSkus
|
||||
* @property ProductCategoryVariantsTable&BelongsTo $ProductCategoryVariants
|
||||
* @property ProductCategoryVariantOptionsTable&BelongsTo $ProductCategoryVariantOptions
|
||||
*
|
||||
* @method ProductSkuVariantValue newEmptyEntity()
|
||||
* @method ProductSkuVariantValue newEntity(array $data, array $options = [])
|
||||
* @method array<ProductSkuVariantValue> newEntities(array $data, array $options = [])
|
||||
* @method ProductSkuVariantValue get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
|
||||
* @method ProductSkuVariantValue findOrCreate($search, ?callable $callback = null, array $options = [])
|
||||
* @method ProductSkuVariantValue patchEntity(EntityInterface $entity, array $data, array $options = [])
|
||||
* @method array<ProductSkuVariantValue> patchEntities(iterable $entities, array $data, array $options = [])
|
||||
* @method ProductSkuVariantValue|false save(EntityInterface $entity, array $options = [])
|
||||
* @method ProductSkuVariantValue saveOrFail(EntityInterface $entity, array $options = [])
|
||||
* @method iterable<ProductSkuVariantValue>|ResultSetInterface<ProductSkuVariantValue>|false saveMany(iterable $entities, array $options = [])
|
||||
* @method iterable<ProductSkuVariantValue>|ResultSetInterface<ProductSkuVariantValue> saveManyOrFail(iterable $entities, array $options = [])
|
||||
* @method iterable<ProductSkuVariantValue>|ResultSetInterface<ProductSkuVariantValue>|false deleteMany(iterable $entities, array $options = [])
|
||||
* @method iterable<ProductSkuVariantValue>|ResultSetInterface<ProductSkuVariantValue> deleteManyOrFail(iterable $entities, array $options = [])
|
||||
*/
|
||||
class ProductSkuVariantValuesTable extends Table
|
||||
{
|
||||
/**
|
||||
* Initialize method
|
||||
*
|
||||
* @param array<string, mixed> $config The configuration for the Table.
|
||||
* @return void
|
||||
*/
|
||||
public function initialize(array $config): void
|
||||
{
|
||||
parent::initialize($config);
|
||||
|
||||
$this->setTable('product_sku_variant_values');
|
||||
$this->setDisplayField('id');
|
||||
$this->setPrimaryKey('id');
|
||||
|
||||
$this->belongsTo('ProductSkus', [
|
||||
'className' => 'CakeProducts.ProductSkus',
|
||||
'foreignKey' => 'product_sku_id',
|
||||
'joinType' => 'INNER',
|
||||
]);
|
||||
$this->belongsTo('ProductCategoryVariants', [
|
||||
'className' => 'CakeProducts.ProductCategoryVariants',
|
||||
'foreignKey' => 'product_category_variant_id',
|
||||
'joinType' => 'INNER',
|
||||
]);
|
||||
$this->belongsTo('ProductCategoryVariantOptions', [
|
||||
'className' => 'CakeProducts.ProductCategoryVariantOptions',
|
||||
'foreignKey' => 'product_category_variant_option_id',
|
||||
'joinType' => 'INNER',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default validation rules.
|
||||
*
|
||||
* @param Validator $validator Validator instance.
|
||||
* @return Validator
|
||||
*/
|
||||
public function validationDefault(Validator $validator): Validator
|
||||
{
|
||||
$validator
|
||||
->uuid('product_sku_id')
|
||||
->notEmptyString('product_sku_id');
|
||||
|
||||
$validator
|
||||
->uuid('product_category_variant_id')
|
||||
->notEmptyString('product_category_variant_id');
|
||||
|
||||
$validator
|
||||
->uuid('product_category_variant_option_id')
|
||||
->notEmptyString('product_category_variant_option_id');
|
||||
|
||||
return $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a rules checker object that will be used for validating
|
||||
* application integrity.
|
||||
*
|
||||
* @param RulesChecker $rules The rules object to be modified.
|
||||
* @return RulesChecker
|
||||
*/
|
||||
public function buildRules(RulesChecker $rules): RulesChecker
|
||||
{
|
||||
$rules->add($rules->existsIn(['product_sku_id'], 'ProductSkus'), ['errorField' => 'product_sku_id']);
|
||||
$rules->add($rules->existsIn(['product_category_variant_id'], 'ProductCategoryVariants'), ['errorField' => 'product_category_variant_id']);
|
||||
$rules->add($rules->existsIn(['product_category_variant_option_id'], 'ProductCategoryVariantOptions'), ['errorField' => 'product_category_variant_option_id']);
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user