-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bake table with enums mapped. (#969)
- Loading branch information
1 parent
206758c
commit e7e3c43
Showing
7 changed files
with
225 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Bake\Test\App\Model\Table; | ||
|
||
use Bake\Test\App\Model\Enum\BakeUserStatus; | ||
use Cake\Database\Type\EnumType; | ||
use Cake\ORM\Table; | ||
use Cake\Validation\Validator; | ||
|
||
/** | ||
* TestBakeArticles Model | ||
* | ||
* @method \Bake\Test\App\Model\Entity\TestBakeArticle newEmptyEntity() | ||
* @method \Bake\Test\App\Model\Entity\TestBakeArticle newEntity(array $data, array $options = []) | ||
* @method array<\Bake\Test\App\Model\Entity\TestBakeArticle> newEntities(array $data, array $options = []) | ||
* @method \Bake\Test\App\Model\Entity\TestBakeArticle get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args) | ||
* @method \Bake\Test\App\Model\Entity\TestBakeArticle findOrCreate($search, ?callable $callback = null, array $options = []) | ||
* @method \Bake\Test\App\Model\Entity\TestBakeArticle patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) | ||
* @method array<\Bake\Test\App\Model\Entity\TestBakeArticle> patchEntities(iterable $entities, array $data, array $options = []) | ||
* @method \Bake\Test\App\Model\Entity\TestBakeArticle|false save(\Cake\Datasource\EntityInterface $entity, array $options = []) | ||
* @method \Bake\Test\App\Model\Entity\TestBakeArticle saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = []) | ||
* @method iterable<\Bake\Test\App\Model\Entity\TestBakeArticle>|\Cake\Datasource\ResultSetInterface<\Bake\Test\App\Model\Entity\TestBakeArticle>|false saveMany(iterable $entities, array $options = []) | ||
* @method iterable<\Bake\Test\App\Model\Entity\TestBakeArticle>|\Cake\Datasource\ResultSetInterface<\Bake\Test\App\Model\Entity\TestBakeArticle> saveManyOrFail(iterable $entities, array $options = []) | ||
* @method iterable<\Bake\Test\App\Model\Entity\TestBakeArticle>|\Cake\Datasource\ResultSetInterface<\Bake\Test\App\Model\Entity\TestBakeArticle>|false deleteMany(iterable $entities, array $options = []) | ||
* @method iterable<\Bake\Test\App\Model\Entity\TestBakeArticle>|\Cake\Datasource\ResultSetInterface<\Bake\Test\App\Model\Entity\TestBakeArticle> deleteManyOrFail(iterable $entities, array $options = []) | ||
* | ||
* @mixin \Cake\ORM\Behavior\TimestampBehavior | ||
*/ | ||
class TestBakeArticlesTable 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('bake_articles'); | ||
$this->setDisplayField('title'); | ||
$this->setPrimaryKey('id'); | ||
|
||
$this->addBehavior('Timestamp'); | ||
|
||
$this->getSchema()->setColumnType('status', EnumType::from(BakeUserStatus::class)); | ||
|
||
$this->belongsTo('BakeUsers', [ | ||
'foreignKey' => 'bake_user_id', | ||
'joinType' => 'INNER', | ||
]); | ||
$this->hasMany('BakeComments', [ | ||
'foreignKey' => 'bake_article_id', | ||
]); | ||
$this->belongsToMany('BakeTags', [ | ||
'foreignKey' => 'bake_article_id', | ||
'targetForeignKey' => 'bake_tag_id', | ||
'joinTable' => 'bake_articles_bake_tags', | ||
]); | ||
} | ||
|
||
/** | ||
* Default validation rules. | ||
* | ||
* @param \Cake\Validation\Validator $validator Validator instance. | ||
* @return \Cake\Validation\Validator | ||
*/ | ||
public function validationDefault(Validator $validator): Validator | ||
{ | ||
$validator | ||
->numeric('id') | ||
->allowEmptyString('id', 'create'); | ||
|
||
$validator | ||
->scalar('name') | ||
->maxLength('name', 100, 'Name must be shorter than 100 characters.') | ||
->requirePresence('name', 'create') | ||
->allowEmptyString('name', null, false); | ||
|
||
$validator | ||
->nonNegativeInteger('count') | ||
->requirePresence('count', 'create') | ||
->allowEmptyString('count', null, false); | ||
|
||
$validator | ||
->greaterThanOrEqual('price', 0) | ||
->requirePresence('price', 'create') | ||
->allowEmptyString('price', null, false); | ||
|
||
$validator | ||
->email('email') | ||
->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']) | ||
->allowEmptyString('email'); | ||
|
||
$validator | ||
->uploadedFile('image', [ | ||
'optional' => true, | ||
'types' => ['image/jpeg'], | ||
]) | ||
->allowEmptyFile('image'); | ||
|
||
return $validator; | ||
} | ||
|
||
/** | ||
* Returns the database connection name to use by default. | ||
* | ||
* @return string | ||
*/ | ||
public static function defaultConnectionName(): string | ||
{ | ||
return 'test'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* @since 3.1.0 | ||
* @license https://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Bake\Test\App\Model\Enum; | ||
|
||
use Cake\Database\Type\EnumLabelInterface; | ||
use Cake\Utility\Inflector; | ||
|
||
enum BakeUserStatus: int implements EnumLabelInterface | ||
{ | ||
case INACTIVE = 0; | ||
case ACTIVE = 1; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function label(): string | ||
{ | ||
return Inflector::humanize(mb_strtolower($this->name)); | ||
} | ||
} |