diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 583f12f..0a11c06 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -55,7 +55,9 @@ public function initialize(array $config): void $schema = $this->_table->getSchema(); /** @var string $field */ foreach (array_keys($this->getConfig()) as $field) { - $schema->setColumnType($field, 'upload.file'); + if ($schema->hasColumn($field)) { + $schema->setColumnType($field, 'upload.file'); + } } $this->_table->setSchema($schema); } diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 465e23a..14b4845 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -68,9 +68,13 @@ public function testInitialize() { $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['setColumnType']) + ->onlyMethods(['setColumnType', 'hasColumn']) ->disableOriginalConstructor() ->getMock(); + $schema->expects($this->once()) + ->method('hasColumn') + ->with('field') + ->willReturn(true); $schema->expects($this->once()) ->method('setColumnType') ->with('field', 'upload.file'); @@ -110,9 +114,13 @@ public function testInitializeIndexedConfig() $settings = ['field']; $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['setColumnType']) + ->onlyMethods(['setColumnType', 'hasColumn']) ->disableOriginalConstructor() ->getMock(); + $schema->expects($this->once()) + ->method('hasColumn') + ->with('field') + ->willReturn(true); $schema->expects($this->once()) ->method('setColumnType') ->with('field', 'upload.file'); @@ -142,9 +150,13 @@ public function testInitializeAddBehaviorOptionsInterfaceConfig() ]; $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['setColumnType']) + ->onlyMethods(['setColumnType', 'hasColumn']) ->disableOriginalConstructor() ->getMock(); + $schema->expects($this->once()) + ->method('hasColumn') + ->with('field') + ->willReturn(true); $schema->expects($this->once()) ->method('setColumnType') ->with('field', 'upload.file'); diff --git a/tests/schema.php b/tests/schema.php index 37da7c8..9eca1f5 100644 --- a/tests/schema.php +++ b/tests/schema.php @@ -7,6 +7,7 @@ 'columns' => [ 'id' => ['type' => 'integer'], 'filename' => ['type' => 'string'], + 'field' => ['type' => 'string'], 'created' => ['type' => 'datetime', 'null' => true], ], 'constraints' => [