CakeProducts/src/Model/Table/ProductPhotosTable.php

137 lines
4.6 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace CakeProducts\Model\Table;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\ResultSetInterface;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Behavior\TimestampBehavior;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use CakeProducts\Model\Entity\ProductPhoto;
use Closure;
use Psr\SimpleCache\CacheInterface;
/**
* ProductPhotos Model
*
* @property ProductsTable&BelongsTo $Products
* @property ProductSkusTable&BelongsTo $ProductSkus
*
* @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 = [])
*
* @mixin TimestampBehavior
*/
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');
$this->setEntityClass(
Configure::read('CakeProducts.ProductPhotos.entity', 'CakeProducts\Model\Entity\ProductPhoto')
);
$this->addBehavior('Timestamp');
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER',
'className' => 'CakeProducts.Products',
]);
$this->belongsTo('ProductSkus', [
'foreignKey' => 'product_sku_id',
'joinType' => 'LEFT',
'className' => 'CakeProducts.ProductSkus',
]);
}
/**
* Default validation rules.
*
* @param Validator $validator Validator instance.
* @return Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->uuid('product_id')
->notEmptyString('product_id');
$validator
->uuid('product_sku_id')
->allowEmptyString('product_sku_id');
$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.
*
* @param RulesChecker $rules The rules object to be modified.
* @return RulesChecker
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']);
$rules->add($rules->existsIn(['product_sku_id'], 'ProductSkus'), ['errorField' => 'product_sku_id']);
return $rules;
}
}