bring into standalone plugin for distribution
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeProducts\Model\Table;
|
||||
|
||||
use Cake\Database\Type\EnumType;
|
||||
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\Product;
|
||||
use CakeProducts\Model\Enum\ProductProductTypeId;
|
||||
use Closure;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
|
||||
/**
|
||||
* Products Model
|
||||
*
|
||||
* @property ProductCategoriesTable&BelongsTo $ProductCategories
|
||||
*
|
||||
* @method Product newEmptyEntity()
|
||||
* @method Product newEntity(array $data, array $options = [])
|
||||
* @method array<Product> newEntities(array $data, array $options = [])
|
||||
* @method Product get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
|
||||
* @method Product findOrCreate($search, ?callable $callback = null, array $options = [])
|
||||
* @method Product patchEntity(EntityInterface $entity, array $data, array $options = [])
|
||||
* @method array<Product> patchEntities(iterable $entities, array $data, array $options = [])
|
||||
* @method Product|false save(EntityInterface $entity, array $options = [])
|
||||
* @method Product saveOrFail(EntityInterface $entity, array $options = [])
|
||||
* @method iterable<Product>|ResultSetInterface<Product>|false saveMany(iterable $entities, array $options = [])
|
||||
* @method iterable<Product>|ResultSetInterface<Product> saveManyOrFail(iterable $entities, array $options = [])
|
||||
* @method iterable<Product>|ResultSetInterface<Product>|false deleteMany(iterable $entities, array $options = [])
|
||||
* @method iterable<Product>|ResultSetInterface<Product> deleteManyOrFail(iterable $entities, array $options = [])
|
||||
*/
|
||||
class ProductsTable 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('products');
|
||||
$this->setDisplayField('name');
|
||||
$this->setPrimaryKey('id');
|
||||
|
||||
$this->belongsTo('ProductCategories', [
|
||||
'foreignKey' => 'product_category_id',
|
||||
'bindingKey' => 'internal_id',
|
||||
'joinType' => 'INNER',
|
||||
'className' => 'CakeProducts.ProductCategories',
|
||||
]);
|
||||
|
||||
$this->getSchema()->setColumnType('product_type_id', EnumType::from(ProductProductTypeId::class));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Default validation rules.
|
||||
*
|
||||
* @param Validator $validator Validator instance.
|
||||
* @return Validator
|
||||
*/
|
||||
public function validationDefault(Validator $validator): Validator
|
||||
{
|
||||
$validator
|
||||
->scalar('name')
|
||||
->maxLength('name', 255)
|
||||
->requirePresence('name', 'create')
|
||||
->notEmptyString('name');
|
||||
|
||||
$validator
|
||||
->uuid('product_category_id')
|
||||
->notEmptyString('product_category_id');
|
||||
|
||||
$validator
|
||||
->integer('product_type_id')
|
||||
->requirePresence('product_type_id', 'create')
|
||||
->notEmptyString('product_type_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->isUnique(['product_category_id', 'name']), ['errorField' => '0']);
|
||||
$rules->add($rules->existsIn(['product_category_id'], 'ProductCategories'), ['errorField' => '1']);
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user