TechFanDan
TechFanDan

Reputation: 3478

Unwanted belongsTo association when baking model where fieldname contains "_id"

When I bake a model that has "_id" in the middle of the fieldname, it will create unwanted belongsTo associations.

Using Cake Bake 2.4.5.

Offending fields:

Table Schema

CREATE TABLE IF NOT EXISTS data_source_names(
  id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) DEFAULT NULL,
  description VARCHAR(255) DEFAULT NULL,
  ora_tsn_service_name VARCHAR(255) DEFAULT NULL,
  ora_enable_result_sets TINYINT(1) DEFAULT NULL,
  ora_enable_query_timeout TINYINT(1) DEFAULT NULL,
  ora_read_only_connection TINYINT(1) DEFAULT NULL,
  ora_enable_closing_cursors TINYINT(1) DEFAULT NULL,
  ora_enable_thread_safety TINYINT(1) DEFAULT NULL,
  ora_batch_autocommit_mode VARCHAR(255) DEFAULT NULL,
  ora_numeric_settings TINYINT(1) DEFAULT NULL,
  ora_fetch_buffer_size TINYINT(1) DEFAULT NULL,
  ora_enable_lobs TINYINT(1) DEFAULT NULL,
  ora_enable_statement_caching TINYINT(1) DEFAULT NULL,
  ora_cache_buffer_size TINYINT(1) DEFAULT NULL,
  ora_enable_failover TINYINT(1) DEFAULT NULL,
  ora_retry INT(10) DEFAULT NULL,
  ora_delay INT(10) DEFAULT NULL,
  ora_bind_timestamp_as_date TINYINT(1) DEFAULT NULL,
  ora_disable_sql_describe_param TINYINT(1) DEFAULT NULL,
  ora_force_sql_wchar_support TINYINT(1) DEFAULT NULL,
  ora_bind_number_as_float TINYINT(1) DEFAULT NULL,
  ora_disable_microsoft_transaction_server TINYINT(1) DEFAULT NULL,
  ora_disable_rule_hint TINYINT(1) DEFAULT NULL,
  ora_set_metadata_id_default_to_sql_true TINYINT(1) DEFAULT NULL,
  ora_enable_exec_syntax TINYINT(1) DEFAULT NULL,
  ora_schema VARCHAR(255) DEFAULT NULL,
  sqlserver_use_sql_server_authentication TINYINT(1) DEFAULT NULL,
  sqlserver_network_libraries VARCHAR(255) DEFAULT NULL,
  sqlserver_dynamically_determine_port TINYINT(1) DEFAULT NULL,
  sqlserver_port_number INT(10) DEFAULT NULL,
  sqlserver_change_the_default_database_to VARCHAR(255) DEFAULT NULL,
  sqlserver_attach_database_filename VARCHAR(255) DEFAULT NULL,
  sqlserver_create_temporary_stored_procedures VARCHAR(255) DEFAULT NULL,
  sqlserver_use_ansi_quoted_identifiers TINYINT(1) DEFAULT NULL,
  sqlserver_use_ansi_nulls_padding_warnings TINYINT(1) DEFAULT NULL,
  sqlserver_use_failover_sql_server TINYINT(1) DEFAULT NULL,
  sqlserver_change_the_language_of_sql_server_system_messages_to VARCHAR(255) DEFAULT NULL,
  sqlserver_use_strong_encryption_for_data TINYINT(1) DEFAULT NULL,
  sqlserver_perform_translation_for_character_data TINYINT(1) DEFAULT NULL,
  sqlserver_use_regional_settings TINYINT(1) DEFAULT NULL,
  sqlserver_save_long_running_queries_to_log_file VARCHAR(255) DEFAULT NULL,
  sqlserver_log_odbc_driver_statistics_to_log_file VARCHAR(255) DEFAULT NULL,
  created DATETIME DEFAULT NULL,
  modified DATETIME DEFAULT NULL,
  created_by INT(10) UNSIGNED DEFAULT NULL,
  modified_by INT(10) UNSIGNED DEFAULT NULL,
  PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET utf8
COLLATE utf8_unicode_ci;

Unwanted belongsTo associations:

/**
 * belongsTo associations
 *
 * @var array
 */
public $belongsTo = array(
    'OraSetMetadataDefaultToSqlTrue' => array(
        'className' => 'OraSetMetadataDefaultToSqlTrue',
        'foreignKey' => 'ora_set_metadata_id_default_to_sql_true',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'SqlserverUseAnsiQuotedentifiers' => array(
        'className' => 'SqlserverUseAnsiQuotedentifiers',
        'foreignKey' => 'sqlserver_use_ansi_quoted_identifiers',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

Upvotes: 0

Views: 39

Answers (1)

ndm
ndm

Reputation: 60463

That's a bug that has been fixed in 2.4.7

https://github.com/cakephp/cakephp/issues/3230

Previously bake would just search for the string _id anywhere in the fieldname (instead at the end of it), which leads to such false findings as you are experiencing.

Upvotes: 1

Related Questions