diff --git a/src/View/Helper/BakeHelper.php b/src/View/Helper/BakeHelper.php
index 52e11dd6..431405b3 100644
--- a/src/View/Helper/BakeHelper.php
+++ b/src/View/Helper/BakeHelper.php
@@ -9,6 +9,8 @@
use Cake\Core\Configure;
use Cake\Core\ConventionsTrait;
use Cake\Database\Schema\TableSchema;
+use Cake\Database\Type\EnumType;
+use Cake\Database\TypeFactory;
use Cake\Datasource\SchemaInterface;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
@@ -281,6 +283,26 @@ public function columnData(string $field, TableSchema $schema): ?array
return $schema->getColumn($field);
}
+ /**
+ * Check if a column is both an enum, and the mapped enum implements `label()` as a method.
+ *
+ * @param string $field the field to check
+ * @param \Cake\Database\Schema\TableSchema $schema The table schema to read from.
+ * @return bool
+ */
+ public function enumSupportsLabel(string $field, TableSchema $schema): bool
+ {
+ $typeName = $schema->getColumnType($field);
+ if (!str_starts_with($typeName, 'enum-')) {
+ return false;
+ }
+ $type = TypeFactory::build($typeName);
+ assert($type instanceof EnumType);
+ $enumClass = $type->getEnumClassName();
+
+ return method_exists($enumClass, 'label');
+ }
+
/**
* Get alias of associated table.
*
diff --git a/templates/bake/Template/index.twig b/templates/bake/Template/index.twig
index 0c630067..dc3852ed 100644
--- a/templates/bake/Template/index.twig
+++ b/templates/bake/Template/index.twig
@@ -49,8 +49,9 @@
{% endif %}
{% if isKey is not same as(true) %}
{% set columnData = Bake.columnData(field, schema) %}
+{% set supportsLabel = Bake.enumSupportsLabel(field, schema) %}
{% if columnData.type starts with 'enum-' %}
-
= ${{ singularVar }}->{{ field }} === null ? '' : h(${{ singularVar }}->{{ field }}->label()) ?>
+
= ${{ singularVar }}->{{ field }} === null ? '' : h(${{ singularVar }}->{{ field }}->{% if supportsLabel %}label(){% else %}value{% endif %}) ?>
{% elseif columnData.type not in ['integer', 'float', 'decimal', 'biginteger', 'smallinteger', 'tinyinteger'] %}
= h(${{ singularVar }}->{{ field }}) ?>
{% elseif columnData.null %}
@@ -81,4 +82,4 @@
= $this->Paginator->counter(__('Page {{ '{{' }}page{{ '}}' }} of {{ '{{' }}pages{{ '}}' }}, showing {{ '{{' }}current{{ '}}' }} record(s) out of {{ '{{' }}count{{ '}}' }} total')) ?>
-
+
\ No newline at end of file
diff --git a/templates/bake/Template/view.twig b/templates/bake/Template/view.twig
index 76192e48..4957300e 100644
--- a/templates/bake/Template/view.twig
+++ b/templates/bake/Template/view.twig
@@ -81,10 +81,11 @@
= __('{{ field|humanize }}') ?>
{% set columnData = Bake.columnData(field, schema) %}
+{% set supportsLabel = Bake.enumSupportsLabel(field, schema) %}
{% if columnData.null %}
-
= ${{ singularVar }}->{{ field }} === null ? '' : h(${{ singularVar }}->{{ field }}->label()) ?>
+
= ${{ singularVar }}->{{ field }} === null ? '' : h(${{ singularVar }}->{{ field }}->{% if supportsLabel %}label(){% else %}value{% endif %}) ?>
{% else %}
-
= h(${{ singularVar }}->{{ field }}->label()) ?>
+
= h(${{ singularVar }}->{{ field }}->{% if supportsLabel %}label(){% else %}value{% endif %}) ?>
{% endif %}
{% endfor %}
@@ -151,4 +152,4 @@
{% endfor %}
-
+
\ No newline at end of file
diff --git a/tests/TestCase/Command/TemplateCommandTest.php b/tests/TestCase/Command/TemplateCommandTest.php
index 0c3aac31..7c0e47b0 100644
--- a/tests/TestCase/Command/TemplateCommandTest.php
+++ b/tests/TestCase/Command/TemplateCommandTest.php
@@ -17,6 +17,8 @@
namespace Bake\Test\TestCase\Command;
use Bake\Command\TemplateCommand;
+use Bake\Test\App\Model\Enum\ArticleStatus;
+use Bake\Test\App\Model\Enum\BakeUserStatus;
use Bake\Test\App\Model\Table\BakeArticlesTable;
use Bake\Test\TestCase\TestCase;
use Cake\Console\Arguments;
@@ -25,6 +27,7 @@
use Cake\Console\Exception\StopException;
use Cake\Core\Configure;
use Cake\Core\Plugin;
+use Cake\Database\Type\EnumType;
use Cake\View\Exception\MissingTemplateException;
/**
@@ -42,6 +45,7 @@ class TemplateCommandTest extends TestCase
'plugin.Bake.Tags',
'plugin.Bake.ArticlesTags',
'plugin.Bake.Posts',
+ 'plugin.Bake.Users',
'plugin.Bake.Comments',
'plugin.Bake.BakeArticles',
'plugin.Bake.BakeTemplateAuthors',
@@ -449,6 +453,47 @@ public function testBakeView()
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}
+ /**
+ * test baking view with enum class
+ *
+ * @return void
+ */
+ public function testBakeViewEnum()
+ {
+ $table = $this->fetchTable('BakeUsers');
+ $table->associations()->removeAll();
+ $table->getSchema()->setColumnType('status', EnumType::from(BakeUserStatus::class));
+
+ $this->generatedFile = ROOT . 'templates/BakeUsers/view.php';
+ $this->exec('bake template bake_users view');
+
+ $this->assertExitCode(CommandInterface::CODE_SUCCESS);
+ $this->assertFileExists($this->generatedFile);
+
+ $result = file_get_contents($this->generatedFile);
+ $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
+ }
+
+ /**
+ * test baking view with enum class
+ *
+ * @return void
+ */
+ public function testBakeViewEnumNoLabel()
+ {
+ $table = $this->fetchTable('Articles');
+ $table->getSchema()->setColumnType('status', EnumType::from(ArticleStatus::class));
+
+ $this->generatedFile = ROOT . 'templates/Articles/view.php';
+ $this->exec('bake template articles view');
+
+ $this->assertExitCode(CommandInterface::CODE_SUCCESS);
+ $this->assertFileExists($this->generatedFile);
+
+ $result = file_get_contents($this->generatedFile);
+ $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
+ }
+
/**
* Test generating view template with hidden fields
*
@@ -551,6 +596,46 @@ public function testBakeIndexWithIndexLimit()
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}
+ /**
+ * test bake template with index enum types
+ *
+ * @return void
+ */
+ public function testBakeIndexWithEnumWithLabel()
+ {
+ $table = $this->fetchTable('BakeUsers');
+ $table->getSchema()->setColumnType('status', EnumType::from(BakeUserStatus::class));
+
+ $this->generatedFile = ROOT . 'templates/BakeUsers/index.php';
+ $this->exec('bake template bake_users index');
+
+ $this->assertExitCode(CommandInterface::CODE_SUCCESS);
+ $this->assertFileExists($this->generatedFile);
+
+ $result = file_get_contents($this->generatedFile);
+ $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
+ }
+
+ /**
+ * test bake template with index enum types
+ *
+ * @return void
+ */
+ public function testBakeIndexWithEnumNoLabel()
+ {
+ $table = $this->fetchTable('Articles');
+ $table->getSchema()->setColumnType('status', EnumType::from(ArticleStatus::class));
+
+ $this->generatedFile = ROOT . 'templates/Articles/index.php';
+ $this->exec('bake template articles index');
+
+ $this->assertExitCode(CommandInterface::CODE_SUCCESS);
+ $this->assertFileExists($this->generatedFile);
+
+ $result = file_get_contents($this->generatedFile);
+ $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
+ }
+
/**
* test Bake with plugins
*
diff --git a/tests/comparisons/Template/testBakeIndex.php b/tests/comparisons/Template/testBakeIndex.php
index 3f6ff809..fe4cc5fa 100644
--- a/tests/comparisons/Template/testBakeIndex.php
+++ b/tests/comparisons/Template/testBakeIndex.php
@@ -49,4 +49,4 @@
= $this->Paginator->counter(__('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')) ?>
-
+
\ No newline at end of file
diff --git a/tests/comparisons/Template/testBakeIndexHiddenFields.php b/tests/comparisons/Template/testBakeIndexHiddenFields.php
index 57f00819..74e551f0 100644
--- a/tests/comparisons/Template/testBakeIndexHiddenFields.php
+++ b/tests/comparisons/Template/testBakeIndexHiddenFields.php
@@ -39,4 +39,4 @@
= $this->Paginator->counter(__('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')) ?>
-
+
\ No newline at end of file
diff --git a/tests/comparisons/Template/testBakeIndexWithEnum.php b/tests/comparisons/Template/testBakeIndexWithEnum.php
new file mode 100644
index 00000000..b5b49745
--- /dev/null
+++ b/tests/comparisons/Template/testBakeIndexWithEnum.php
@@ -0,0 +1,50 @@
+ $bakeUsers
+ */
+?>
+
= $this->Paginator->counter(__('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')) ?>
+
+
\ No newline at end of file
diff --git a/tests/comparisons/Template/testBakeIndexWithIndexLimit.php b/tests/comparisons/Template/testBakeIndexWithIndexLimit.php
index 34b8f8ca..fd6a3bf6 100644
--- a/tests/comparisons/Template/testBakeIndexWithIndexLimit.php
+++ b/tests/comparisons/Template/testBakeIndexWithIndexLimit.php
@@ -43,4 +43,4 @@
= $this->Paginator->counter(__('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')) ?>
-
+
\ No newline at end of file
diff --git a/tests/comparisons/Template/testBakeView.php b/tests/comparisons/Template/testBakeView.php
index 7e37823b..6a84c762 100644
--- a/tests/comparisons/Template/testBakeView.php
+++ b/tests/comparisons/Template/testBakeView.php
@@ -94,4 +94,4 @@
-
+
\ No newline at end of file
diff --git a/tests/comparisons/Template/testBakeViewEnum.php b/tests/comparisons/Template/testBakeViewEnum.php
new file mode 100644
index 00000000..4cae270b
--- /dev/null
+++ b/tests/comparisons/Template/testBakeViewEnum.php
@@ -0,0 +1,44 @@
+
+
\ No newline at end of file
diff --git a/tests/comparisons/Template/testBakeViewEnumNoLabel.php b/tests/comparisons/Template/testBakeViewEnumNoLabel.php
new file mode 100644
index 00000000..e5e0fd4b
--- /dev/null
+++ b/tests/comparisons/Template/testBakeViewEnumNoLabel.php
@@ -0,0 +1,75 @@
+
+
\ No newline at end of file
diff --git a/tests/comparisons/Template/testBakeViewHiddenFields.php b/tests/comparisons/Template/testBakeViewHiddenFields.php
index add7cb22..441738d8 100644
--- a/tests/comparisons/Template/testBakeViewHiddenFields.php
+++ b/tests/comparisons/Template/testBakeViewHiddenFields.php
@@ -25,4 +25,4 @@
-
+
\ No newline at end of file
diff --git a/tests/comparisons/Template/testBakeViewWithEnum.php b/tests/comparisons/Template/testBakeViewWithEnum.php
new file mode 100644
index 00000000..e69de29b
diff --git a/tests/comparisons/Template/testGetContent.php b/tests/comparisons/Template/testGetContent.php
index 0b454513..f9b363f7 100644
--- a/tests/comparisons/Template/testGetContent.php
+++ b/tests/comparisons/Template/testGetContent.php
@@ -33,4 +33,4 @@
-
+
\ No newline at end of file
diff --git a/tests/comparisons/Template/testGetContentAssociations.php b/tests/comparisons/Template/testGetContentAssociations.php
index a9cdb9ad..689c58ca 100644
--- a/tests/comparisons/Template/testGetContentAssociations.php
+++ b/tests/comparisons/Template/testGetContentAssociations.php
@@ -33,4 +33,4 @@
-
+
\ No newline at end of file
diff --git a/tests/comparisons/Template/testGetContentWithRoutingPrefix-view.php b/tests/comparisons/Template/testGetContentWithRoutingPrefix-view.php
index 2521bf4b..6d3a8042 100644
--- a/tests/comparisons/Template/testGetContentWithRoutingPrefix-view.php
+++ b/tests/comparisons/Template/testGetContentWithRoutingPrefix-view.php
@@ -35,4 +35,4 @@
-
+
\ No newline at end of file