2025-08-10 09:35:25 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace CakeProducts\Model\Table;
|
|
|
|
|
2025-09-01 07:46:18 +00:00
|
|
|
use Cake\Core\Configure;
|
2025-08-12 06:35:46 +00:00
|
|
|
use Cake\Datasource\EntityInterface;
|
|
|
|
use Cake\Datasource\ResultSetInterface;
|
|
|
|
use Cake\ORM\Association\BelongsTo;
|
|
|
|
use Cake\ORM\Behavior\TimestampBehavior;
|
2025-08-10 09:35:25 +00:00
|
|
|
use Cake\ORM\RulesChecker;
|
|
|
|
use Cake\ORM\Table;
|
|
|
|
use Cake\Validation\Validator;
|
2025-08-12 06:35:46 +00:00
|
|
|
use CakeProducts\Model\Entity\ProductPhoto;
|
|
|
|
use Closure;
|
|
|
|
use Psr\SimpleCache\CacheInterface;
|
2025-08-10 09:35:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ProductPhotos Model
|
|
|
|
*
|
2025-08-12 06:35:46 +00:00
|
|
|
* @property ProductsTable&BelongsTo $Products
|
2025-08-22 06:40:00 +00:00
|
|
|
* @property ProductSkusTable&BelongsTo $ProductSkus
|
2025-08-10 09:35:25 +00:00
|
|
|
*
|
2025-08-12 06:35:46 +00:00
|
|
|
* @method ProductPhoto newEmptyEntity()
|
|
|
|
* @method ProductPhoto newEntity(array $data, array $options = [])
|
|
|
|
* @method array<ProductPhoto> newEntities(array $data, array $options = [])
|
|
|
|
* @method ProductPhoto get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
|
|
|
|
* @method ProductPhoto findOrCreate($search, ?callable $callback = null, array $options = [])
|
|
|
|
* @method ProductPhoto patchEntity(EntityInterface $entity, array $data, array $options = [])
|
|
|
|
* @method array<ProductPhoto> patchEntities(iterable $entities, array $data, array $options = [])
|
|
|
|
* @method ProductPhoto|false save(EntityInterface $entity, array $options = [])
|
|
|
|
* @method ProductPhoto saveOrFail(EntityInterface $entity, array $options = [])
|
|
|
|
* @method iterable<ProductPhoto>|ResultSetInterface<ProductPhoto>|false saveMany(iterable $entities, array $options = [])
|
|
|
|
* @method iterable<ProductPhoto>|ResultSetInterface<ProductPhoto> saveManyOrFail(iterable $entities, array $options = [])
|
|
|
|
* @method iterable<ProductPhoto>|ResultSetInterface<ProductPhoto>|false deleteMany(iterable $entities, array $options = [])
|
|
|
|
* @method iterable<ProductPhoto>|ResultSetInterface<ProductPhoto> deleteManyOrFail(iterable $entities, array $options = [])
|
2025-08-10 09:35:25 +00:00
|
|
|
*
|
2025-08-12 06:35:46 +00:00
|
|
|
* @mixin TimestampBehavior
|
2025-08-10 09:35:25 +00:00
|
|
|
*/
|
|
|
|
class ProductPhotosTable 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_photos');
|
|
|
|
$this->setDisplayField('photo_filename');
|
|
|
|
$this->setPrimaryKey('id');
|
|
|
|
|
2025-09-01 07:46:18 +00:00
|
|
|
$this->setEntityClass(
|
|
|
|
Configure::read('CakeProducts.ProductPhotos.entity', 'CakeProducts\Model\Entity\ProductPhoto')
|
|
|
|
);
|
|
|
|
|
2025-08-10 09:35:25 +00:00
|
|
|
$this->addBehavior('Timestamp');
|
|
|
|
|
|
|
|
$this->belongsTo('Products', [
|
|
|
|
'foreignKey' => 'product_id',
|
|
|
|
'joinType' => 'INNER',
|
|
|
|
'className' => 'CakeProducts.Products',
|
|
|
|
]);
|
2025-08-12 06:35:46 +00:00
|
|
|
|
|
|
|
$this->belongsTo('ProductSkus', [
|
|
|
|
'foreignKey' => 'product_sku_id',
|
|
|
|
'joinType' => 'LEFT',
|
|
|
|
'className' => 'CakeProducts.ProductSkus',
|
|
|
|
]);
|
2025-08-10 09:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default validation rules.
|
|
|
|
*
|
2025-08-12 06:35:46 +00:00
|
|
|
* @param Validator $validator Validator instance.
|
|
|
|
* @return Validator
|
2025-08-10 09:35:25 +00:00
|
|
|
*/
|
|
|
|
public function validationDefault(Validator $validator): Validator
|
|
|
|
{
|
|
|
|
$validator
|
|
|
|
->uuid('product_id')
|
|
|
|
->notEmptyString('product_id');
|
|
|
|
|
|
|
|
$validator
|
|
|
|
->uuid('product_sku_id')
|
2025-08-12 06:35:46 +00:00
|
|
|
->allowEmptyString('product_sku_id');
|
2025-08-10 09:35:25 +00:00
|
|
|
|
|
|
|
$validator
|
|
|
|
->scalar('photo_dir')
|
|
|
|
->maxLength('photo_dir', 255)
|
|
|
|
->requirePresence('photo_dir', 'create')
|
|
|
|
->notEmptyString('photo_dir');
|
|
|
|
|
|
|
|
$validator
|
|
|
|
->scalar('photo_filename')
|
|
|
|
->maxLength('photo_filename', 255)
|
|
|
|
->requirePresence('photo_filename', 'create')
|
|
|
|
->notEmptyString('photo_filename');
|
|
|
|
|
|
|
|
$validator
|
|
|
|
->boolean('primary_photo')
|
|
|
|
->notEmptyString('primary_photo');
|
|
|
|
|
|
|
|
$validator
|
|
|
|
->integer('photo_position')
|
|
|
|
->notEmptyString('photo_position');
|
|
|
|
|
|
|
|
$validator
|
|
|
|
->boolean('enabled')
|
|
|
|
->notEmptyString('enabled');
|
|
|
|
|
|
|
|
$validator
|
|
|
|
->dateTime('deleted')
|
|
|
|
->allowEmptyDateTime('deleted');
|
|
|
|
|
|
|
|
return $validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a rules checker object that will be used for validating
|
|
|
|
* application integrity.
|
|
|
|
*
|
2025-08-12 06:35:46 +00:00
|
|
|
* @param RulesChecker $rules The rules object to be modified.
|
|
|
|
* @return RulesChecker
|
2025-08-10 09:35:25 +00:00
|
|
|
*/
|
|
|
|
public function buildRules(RulesChecker $rules): RulesChecker
|
|
|
|
{
|
2025-08-12 06:35:46 +00:00
|
|
|
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']);
|
|
|
|
$rules->add($rules->existsIn(['product_sku_id'], 'ProductSkus'), ['errorField' => 'product_sku_id']);
|
2025-08-10 09:35:25 +00:00
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
}
|