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 patchEntities(iterable $entities, array $data, array $options = []) * @method Product|false save(EntityInterface $entity, array $options = []) * @method Product saveOrFail(EntityInterface $entity, array $options = []) * @method iterable|ResultSetInterface|false saveMany(iterable $entities, array $options = []) * @method iterable|ResultSetInterface saveManyOrFail(iterable $entities, array $options = []) * @method iterable|ResultSetInterface|false deleteMany(iterable $entities, array $options = []) * @method iterable|ResultSetInterface deleteManyOrFail(iterable $entities, array $options = []) */ class ProductsTable extends Table { /** * Initialize method * * @param array $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->setEntityClass( Configure::read('CakeProducts.Products.entity', 'CakeProducts\Model\Entity\Product') ); $this->belongsTo('ProductCategories', [ 'foreignKey' => 'product_category_id', 'bindingKey' => 'internal_id', 'joinType' => 'INNER', 'className' => 'CakeProducts.ProductCategories', ]); $this->hasMany('ProductAttributes', [ 'className' => 'CakeProducts.ProductAttributes', 'dependent' => true, 'cascadeCallbacks' => true, ]); $this->hasMany('ProductCategoryVariants', [ 'foreignKey' => 'product_id', 'className' => 'CakeProducts.ProductCategoryVariants', 'dependent' => true, 'cascadeCallbacks' => true, ]); $this->hasMany('ProductSkus', [ 'foreignKey' => 'product_id', 'className' => 'CakeProducts.ProductSkus', 'dependent' => true, 'cascadeCallbacks' => true, ]); $this->getSchema()->setColumnType('product_type_id', EnumType::from(ProductProductTypeId::class)); $this->addBehavior('Muffin/Trash.Trash'); } /** * 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'); $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->isUnique(['product_category_id', 'name']), ['errorField' => 'product_category_id']); $rules->add($rules->existsIn(['product_category_id'], 'ProductCategories'), ['errorField' => 'product_category_id']); // $rules->add($rules->validCount('product_attributes', 0, '<=', 'You must not have any tags')); return $rules; } }