toggle behavior on skus for default product sku, photos for default category photo and default product photo
This commit is contained in:
37
src/Model/Behavior/SecondToggleBehavior.php
Normal file
37
src/Model/Behavior/SecondToggleBehavior.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace CakeProducts\Model\Behavior;
|
||||
|
||||
use ArrayObject;
|
||||
use Cake\Datasource\EntityInterface;
|
||||
use Cake\Event\EventInterface;
|
||||
use Cake\ORM\Behavior;
|
||||
use LogicException;
|
||||
use Tools\Model\Behavior\ToggleBehavior;
|
||||
|
||||
/**
|
||||
* ToggleBehavior
|
||||
*
|
||||
* An implementation of a unique field toggle per table or scope.
|
||||
* This will ensure that on a set of records only one can be a "primary" one, setting the others to false then.
|
||||
* On delete it will give the primary status to another record if applicable.
|
||||
*
|
||||
* @author Mark Scherer
|
||||
* @license MIT
|
||||
*/
|
||||
class SecondToggleBehavior extends ToggleBehavior {
|
||||
|
||||
/**
|
||||
* Default config
|
||||
*
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
protected array $_defaultConfig = [
|
||||
'field' => 'primary',
|
||||
'on' => 'afterSave', // afterSave (without transactions) or beforeSave (with transactions)
|
||||
'scopeFields' => [],
|
||||
'scope' => [],
|
||||
'findOrder' => null, // null = autodetect modified/created, false to disable
|
||||
'implementedMethods' => [], // to prevent conflict with public toggleField method
|
||||
];
|
||||
}
|
||||
@@ -58,7 +58,22 @@ class ProductPhotosTable extends Table
|
||||
);
|
||||
|
||||
$this->addBehavior('Timestamp');
|
||||
|
||||
$this->addBehavior('Tools.Toggle', [
|
||||
'field' => 'primary_photo',
|
||||
'scopeFields' => ['product_id'],
|
||||
'scope' => [
|
||||
'deleted IS' => null,
|
||||
'product_id IS NOT' => null,
|
||||
],
|
||||
]);
|
||||
$this->addBehavior('CakeProducts.SecondToggle', [
|
||||
'field' => 'primary_category_photo',
|
||||
'scopeFields' => ['product_category_id'],
|
||||
'scope' => [
|
||||
'deleted IS' => null,
|
||||
'product_category_id IS NOT' => null,
|
||||
],
|
||||
]);
|
||||
$this->belongsTo('Products', [
|
||||
'foreignKey' => 'product_id',
|
||||
'joinType' => 'LEFT',
|
||||
|
||||
@@ -59,6 +59,13 @@ class ProductSkusTable extends Table
|
||||
);
|
||||
|
||||
$this->addBehavior('Timestamp');
|
||||
$this->addBehavior('Tools.Toggle', [
|
||||
'field' => 'default_sku',
|
||||
'scopeFields' => ['product_id'],
|
||||
'scope' => [
|
||||
'deleted IS' => null,
|
||||
],
|
||||
]);
|
||||
|
||||
$this->belongsTo('Products', [
|
||||
'className' => 'CakeProducts.Products',
|
||||
|
||||
Reference in New Issue
Block a user