86 lines
3.5 KiB
PHP
86 lines
3.5 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Model\Table;
|
||
|
||
use Cake\ORM\Query\SelectQuery;
|
||
use Cake\ORM\RulesChecker;
|
||
use Cake\ORM\Table;
|
||
use Cake\Validation\Validator;
|
||
|
||
/**
|
||
* Tools Model
|
||
*
|
||
* @method \App\Model\Entity\Tool newEmptyEntity()
|
||
* @method \App\Model\Entity\Tool newEntity(array $data, array $options = [])
|
||
* @method array<\App\Model\Entity\Tool> newEntities(array $data, array $options = [])
|
||
* @method \App\Model\Entity\Tool get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
|
||
* @method \App\Model\Entity\Tool findOrCreate($search, ?callable $callback = null, array $options = [])
|
||
* @method \App\Model\Entity\Tool patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
|
||
* @method array<\App\Model\Entity\Tool> patchEntities(iterable $entities, array $data, array $options = [])
|
||
* @method \App\Model\Entity\Tool|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
|
||
* @method \App\Model\Entity\Tool saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
|
||
* @method iterable<\App\Model\Entity\Tool>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Tool>|false saveMany(iterable $entities, array $options = [])
|
||
* @method iterable<\App\Model\Entity\Tool>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Tool> saveManyOrFail(iterable $entities, array $options = [])
|
||
* @method iterable<\App\Model\Entity\Tool>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Tool>|false deleteMany(iterable $entities, array $options = [])
|
||
* @method iterable<\App\Model\Entity\Tool>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\Tool> deleteManyOrFail(iterable $entities, array $options = [])
|
||
*
|
||
* @mixin \Cake\ORM\Behavior\TimestampBehavior
|
||
*/
|
||
class ToolsTable extends Table
|
||
{
|
||
//Metoda initialize() to konfiguracja modelu
|
||
// setTable oznacza, ze nazwa tabeli to tools.
|
||
// setDisplayField pole, które reprezentuje tabele w listach.
|
||
// setPrimaryKey ustawia klucz główny.
|
||
// addBehavior ustawia pola timestamp
|
||
/**
|
||
* 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('tools');
|
||
$this->setDisplayField('name');
|
||
$this->setPrimaryKey('id');
|
||
|
||
$this->addBehavior('Timestamp');
|
||
}
|
||
|
||
// Metoda validationDefault to walidacja danych
|
||
// Walidacja przy tworzeniu nowych encji (create)
|
||
// przy aktualizacji (patchEntity), przy zapisie (save)
|
||
/**
|
||
* Default validation rules.
|
||
*
|
||
* @param \Cake\Validation\Validator $validator Validator instance.
|
||
* @return \Cake\Validation\Validator
|
||
*/
|
||
public function validationDefault(Validator $validator): Validator
|
||
{
|
||
$validator
|
||
->scalar('name') // nazwa pola
|
||
->maxLength('name', 150) // string, maks. 150 znakow,
|
||
->requirePresence('name', 'create') // podczas tworzenia
|
||
->notEmptyString('name'); // nie mo˙ ze by´ c puste
|
||
|
||
$validator
|
||
->scalar('description')
|
||
->allowEmptyString('description'); // mo˙ ze by´ c puste
|
||
|
||
$validator
|
||
->integer('quantity')
|
||
->notEmptyString('quantity');
|
||
|
||
$validator
|
||
->boolean('active')
|
||
->notEmptyString('active');
|
||
|
||
return $validator;
|
||
}
|
||
}
|