product skus v1
This commit is contained in:
45
src/Model/Entity/ProductSku.php
Normal file
45
src/Model/Entity/ProductSku.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeProducts\Model\Entity;
|
||||
|
||||
use Cake\ORM\Entity;
|
||||
|
||||
/**
|
||||
* ProductSkus Entity
|
||||
*
|
||||
* @property string $id
|
||||
* @property string $product_id
|
||||
* @property string $sku
|
||||
* @property string|null $barcode
|
||||
* @property string|null $price
|
||||
* @property string|null $cost
|
||||
* @property \Cake\I18n\DateTime $created
|
||||
* @property \Cake\I18n\DateTime|null $modified
|
||||
* @property \Cake\I18n\DateTime|null $deleted
|
||||
*
|
||||
* @property \App\Model\Entity\Product $product
|
||||
*/
|
||||
class ProductSku extends Entity
|
||||
{
|
||||
/**
|
||||
* Fields that can be mass assigned using newEntity() or patchEntity().
|
||||
*
|
||||
* Note that when '*' is set to true, this allows all unspecified fields to
|
||||
* be mass assigned. For security purposes, it is advised to set '*' to false
|
||||
* (or remove it), and explicitly make individual fields accessible as needed.
|
||||
*
|
||||
* @var array<string, bool>
|
||||
*/
|
||||
protected array $_accessible = [
|
||||
'product_id' => true,
|
||||
'sku' => true,
|
||||
'barcode' => true,
|
||||
'price' => true,
|
||||
'cost' => true,
|
||||
'created' => true,
|
||||
'modified' => true,
|
||||
'deleted' => true,
|
||||
'product' => false,
|
||||
];
|
||||
}
|
||||
114
src/Model/Table/ProductSkusTable.php
Normal file
114
src/Model/Table/ProductSkusTable.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeProducts\Model\Table;
|
||||
|
||||
use CakeProducts\Model\Table\ProductsTable;
|
||||
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\ProductSku;
|
||||
use Closure;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
|
||||
/**
|
||||
* ProductSkus Model
|
||||
*
|
||||
* @property ProductsTable&BelongsTo $Products
|
||||
*
|
||||
* @method ProductSku newEmptyEntity()
|
||||
* @method ProductSku newEntity(array $data, array $options = [])
|
||||
* @method array<ProductSku> newEntities(array $data, array $options = [])
|
||||
* @method ProductSku get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
|
||||
* @method ProductSku findOrCreate($search, ?callable $callback = null, array $options = [])
|
||||
* @method ProductSku patchEntity(EntityInterface $entity, array $data, array $options = [])
|
||||
* @method array<ProductSku> patchEntities(iterable $entities, array $data, array $options = [])
|
||||
* @method ProductSku|false save(EntityInterface $entity, array $options = [])
|
||||
* @method ProductSku saveOrFail(EntityInterface $entity, array $options = [])
|
||||
* @method iterable<ProductSku>|ResultSetInterface<ProductSku>|false saveMany(iterable $entities, array $options = [])
|
||||
* @method iterable<ProductSku>|ResultSetInterface<ProductSku> saveManyOrFail(iterable $entities, array $options = [])
|
||||
* @method iterable<ProductSku>|ResultSetInterface<ProductSku>|false deleteMany(iterable $entities, array $options = [])
|
||||
* @method iterable<ProductSku>|ResultSetInterface<ProductSku> deleteManyOrFail(iterable $entities, array $options = [])
|
||||
*
|
||||
* @mixin TimestampBehavior
|
||||
*/
|
||||
class ProductSkusTable 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_skus');
|
||||
$this->setDisplayField('sku');
|
||||
$this->setPrimaryKey('id');
|
||||
|
||||
$this->addBehavior('Timestamp');
|
||||
|
||||
$this->belongsTo('Products', [
|
||||
'foreignKey' => 'product_id',
|
||||
'joinType' => 'INNER',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default validation rules.
|
||||
*
|
||||
* @param Validator $validator Validator instance.
|
||||
* @return Validator
|
||||
*/
|
||||
public function validationDefault(Validator $validator): Validator
|
||||
{
|
||||
$validator
|
||||
->uuid('product_id')
|
||||
->notEmptyString('product_id');
|
||||
|
||||
$validator
|
||||
->scalar('sku')
|
||||
->maxLength('sku', 255)
|
||||
->requirePresence('sku', 'create')
|
||||
->notEmptyString('sku');
|
||||
|
||||
$validator
|
||||
->scalar('barcode')
|
||||
->maxLength('barcode', 255)
|
||||
->allowEmptyString('barcode');
|
||||
|
||||
$validator
|
||||
->decimal('price')
|
||||
->allowEmptyString('price');
|
||||
|
||||
$validator
|
||||
->decimal('cost')
|
||||
->allowEmptyString('cost');
|
||||
|
||||
$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']);
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user