primary sku photo working with janky toggle workaround but w/e lol
This commit is contained in:
57
src/Model/Behavior/ThirdToggleBehavior.php
Normal file
57
src/Model/Behavior/ThirdToggleBehavior.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?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 ThirdToggleBehavior 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
|
||||
];
|
||||
|
||||
/**
|
||||
* @param \Cake\Datasource\EntityInterface $entity
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function buildConditions(EntityInterface $entity) {
|
||||
$conditions = $this->getConfig('scope');
|
||||
$scopeFields = (array)$this->getConfig('scopeFields');
|
||||
|
||||
foreach ($scopeFields as $scopeField) {
|
||||
if ($entity->get($scopeField) === null) {
|
||||
continue;
|
||||
}
|
||||
$conditions[$scopeField] = $entity->get($scopeField);
|
||||
}
|
||||
// dd($conditions);
|
||||
|
||||
return $conditions;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ use Cake\ORM\Entity;
|
||||
* @property string $photo_filename
|
||||
* @property bool $primary_photo
|
||||
* @property bool $primary_category_photo
|
||||
* @property bool $primary_sku_photo
|
||||
* @property int $photo_position
|
||||
* @property bool $enabled
|
||||
* @property DateTime $created
|
||||
@@ -46,6 +47,7 @@ class ProductPhoto extends Entity
|
||||
'photo_filename' => true,
|
||||
'primary_photo' => true,
|
||||
'primary_category_photo' => true,
|
||||
'primary_sku_photo' => true,
|
||||
'photo_position' => true,
|
||||
'enabled' => true,
|
||||
'created' => true,
|
||||
|
||||
@@ -58,6 +58,7 @@ class ProductPhotosTable extends Table
|
||||
);
|
||||
|
||||
$this->addBehavior('Timestamp');
|
||||
|
||||
$this->addBehavior('Tools.Toggle', [
|
||||
'field' => 'primary_photo',
|
||||
'scopeFields' => ['product_id'],
|
||||
@@ -74,6 +75,14 @@ class ProductPhotosTable extends Table
|
||||
'product_category_id IS NOT' => null,
|
||||
],
|
||||
]);
|
||||
$this->addBehavior('CakeProducts.ThirdToggle', [
|
||||
'field' => 'primary_sku_photo',
|
||||
'scopeFields' => ['product_sku_id'],
|
||||
'scope' => [
|
||||
'deleted IS' => null,
|
||||
'product_sku_id IS NOT' => null,
|
||||
],
|
||||
]);
|
||||
$this->belongsTo('Products', [
|
||||
'foreignKey' => 'product_id',
|
||||
'joinType' => 'LEFT',
|
||||
|
||||
Reference in New Issue
Block a user