0

I set up more than one models, which i want to associate with a master-model like this:

class CommonType extends AppModel {
    public $useDbConfig = 'common';

    public $hasOne = array(
        'CommonTypeDescription' => array(
            'className' => 'CommonTypeDescription',
            'foreignKey' => 'type_description_id',
            'dependent' => true
        ),
        'CommonTypeExperience' => array(
            'className' => 'CommonTypeExperience',
            'foreignKey' => 'type_expirience_id',
            'dependent' => true             
        ),
        'CommonTypeProperty' => array(
            'className' => 'CommonTypeProperty',
            'foreignKey' => 'type_property_id',
            'dependent' => true         
        ),
    );

    public $belongsTo = array(
        'CommonTypeCategory' => array(
            'className' => 'CommonTypeCategory',
            'foreignKey' => 'common_type_id',
    ));

}

In Heidi SQL (for example) i set the foreignkeys for the tables type_experiences, type_properties and type_descriptions as above, so that foreignkeys in my code and in my tables have the same name.

But now, i don't want to name the primary key as the foreign key, the primary key of the (e.g.) type_descriptions table should named "id". This is the model of type_description:

class CommonTypeDescription extends AppModel {
   public $name = 'CommonTypeDescription';
   public $useDbConfig = 'common';
   public $useTable = 'type_descriptions';
   public $primaryKey = 'id';

}

and here is the database creation code:

CREATE TABLE `type_descriptions` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`common_type_id` INT(11) NULL DEFAULT NULL,
`short_description` VARCHAR(50) NULL DEFAULT NULL,
`long_description` VARCHAR(50) NULL DEFAULT NULL,
`notes` VARCHAR(50) NULL DEFAULT NULL,
`modified` DATETIME NULL DEFAULT NULL,
`created` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `type_description_id` FOREIGN KEY (`common_type_id`) REFERENCES `common_types` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION) ...

But when i try to get the data from my model CommonType ($this->find('all')) i get an error like "Unknown column 'CommonTypeDescription.type_description_id' in 'on clause'"

First, i thought that i have to declare the primary key of the description model as "id", but it seams, that the foreign key definition overwrites the naming convention from cake of the primary key from the associated model.

Many thanks for your help & Greetings, Guido

ps. i've changed the Model-Names of this post (if there is a type-mistake). So when i rename the primary keys of the associated tables, it all works fine, but i want them to name only to "id".

1 Answers1

0

How about update your CommonType Model to this:

   'CommonTypeDescription' => array(
        'className' => 'CommonTypeDescription',
        'foreignKey' => 'common_type_id', //or try type_description_id if there's an error
        'dependent' => true
    )

Then on your CommonTypeDescription model add this relationship, I think you forgot adding belongs to relationship to CommonType Model:

    public $belongsTo = array(
        'CommonType' => array(
        'className' => 'CommonType ',
        'foreignKey' => 'common_type_id',
    ));
decodingpanda
  • 594
  • 9
  • 18
  • Thank you, this solution has revealed my mistake. I was of the opinion that the foreign key of Cake with the foreign key in a database is dependent on each other. Now I have added the dependency in both models and defined the foreign key with cakeconventions. And it works. – user3803995 Jul 04 '14 at 08:57