CakePHP 3 : Association property name clashes with field of same name of table
Asked Answered
L

2

6

I am using CakePHP 3.2. I have two tables service_requests and coupon_history

service_requests table

CREATE TABLE `service_requests` (
  `id` char(36) NOT NULL,
  `request_id` bigint(20) NOT NULL,
  `user_id` char(36) NOT NULL,
  `user_address_id` char(36) NOT NULL,
  `service_id` char(36) NOT NULL,
  `service_area_id` char(36) NOT NULL,
  `coupon_used` int(11) NOT NULL DEFAULT '0' COMMENT '0: Not Applied; 1: Applied',
  `coupon_used_id` char(36) NOT NULL,
  `status_code` int(11) NOT NULL,
  `status` varchar(50) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL
)

coupon_history table

CREATE TABLE `coupon_history` (
  `id` char(36) NOT NULL,
  `user_id` char(36) NOT NULL,
  `coupon_id` char(36) NOT NULL,
  `service_request_id` char(36) DEFAULT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` datetime NOT NULL
)

When I bake service_requests table by

bin/cake bake all service_requests

It Warning Error as

Association property name "coupon_used" clashes with field of same name of table "service_requests". You should explicitly specify the "propertyName" option. in [/var/www/html/flitfix/admin2/vendor/cakephp/cakephp/src/ORM/Association.php, line 445]

2016-07-01 07:17:32 Warning: Warning (512): Association property name "coupon_used" clashes with field of same name of table "service_requests". You should explicitly specify the "propertyName" option. in [/var/www/html/flitfix/admin2/vendor/cakephp/cakephp/src/ORM/Association.php, line 445]
Trace:
Cake\Error\BaseErrorHandler::handleError() - CORE/src/Error/BaseErrorHandler.php, line 146
Cake\ORM\Association::property() - CORE/src/ORM/Association.php, line 445
Bake\Utility\Model\AssociationFilter::filterAssociations() - ROOT/vendor/cakephp/bake/src/Utility/Model/AssociationFilter.php, line 94
Bake\Shell\Task\TemplateTask::_filteredAssociations() - ROOT/vendor/cakephp/bake/src/Shell/Task/TemplateTask.php, line 465
Bake\Shell\Task\TemplateTask::_loadController() - ROOT/vendor/cakephp/bake/src/Shell/Task/TemplateTask.php, line 295
Bake\Shell\Task\TemplateTask::main() - ROOT/vendor/cakephp/bake/src/Shell/Task/TemplateTask.php, line 147
Bake\Shell\BakeShell::Bake\Shell\{closure}() - ROOT/vendor/cakephp/bake/src/Shell/BakeShell.php, line 252
Cake\Collection\Collection::each() - CORE/src/Collection/CollectionTrait.php, line 51
Bake\Shell\BakeShell::all() - ROOT/vendor/cakephp/bake/src/Shell/BakeShell.php, line 253
Cake\Console\Shell::runCommand() - CORE/src/Console/Shell.php, line 444
Cake\Console\ShellDispatcher::_dispatch() - CORE/src/Console/ShellDispatcher.php, line 221
Cake\Console\ShellDispatcher::dispatch() - CORE/src/Console/ShellDispatcher.php, line 181
Cake\Console\ShellDispatcher::run() - CORE/src/Console/ShellDispatcher.php, line 127
[main] - ROOT/bin/cake.php, line 33

Previously there was coupon_used table which I renamed to coupon_history and deleted all controller, model and template named coupon_used and trying to bake again coupon_history but it gives error. I tried with a new project created by composer but in that too same error is there.

Limbert answered 1/7, 2016 at 7:25 Comment(2)
Read the error message again, it clearly tells you what is wrong and how to fix it.Granite
Yes I can fix the problem as given in error message but the question is why this error even if there is no table coupon_used which conflict with column with same name in service_requestsLimbert
U
6

as warning says, there is conflict beetween field name "coupon_used" with coupon_useds table property, which is coupon_used. this is happen because default rules of CakePHP.

you should change either field name "coupon_used" or coupun_useds table property with another name in service_requests table model.

class ServiceRequests extends Table{
   ....
   //assumption if association is belongsTo
   $this->belongsTo('CouponUsed',['propertyName' => 'AnotherCouponUsed']);
   ....
}
Underfeed answered 5/7, 2016 at 5:14 Comment(2)
Initially there was coupon_used field name in service_requests and coupon_used table. Then I can understand the cause of error. But now, I have deleted coupon_used table and baking application from start creating a new project. Event the error is there. How could it get in conflict even if there is no table by same column nameLimbert
so we need some clue about your database table list, service_requests controller and table model codesUnderfeed
B
1

Please refer below link. Property name to be defined like this.

$this->belongsTo('ProductPrices', [
    'foreignKey' => 'product_price_id',
    'className'  => 'ProductPrices',
    'propertyName' => 'prod_price'
]);

http://www.cloudypoint.com/Tutorials/discussion/cake-framework-solved-cakephp-3-querybuilder-for-associative-data/

Bordello answered 4/7, 2017 at 10:11 Comment(1)
That answer actually points back to SO. Probably better to just mention the update in a comment rather than copying answers from other questions.Authorized

© 2022 - 2024 — McMap. All rights reserved.