简体   繁体   中英

CakePHP Joining same model twice

I have the following tables :

domains (id, name)
alignments (id, name, description)
alignments_domains(id, alignment_id, domain_id)
domains_domains(id, domain_id, authorized_domain_id)

All my foreign keys are made and what I'm trying to achieve is to have several authorized domains and several authorized alignments for each domain.

Thing is, when I cake bake model, controller and view I have this model :

class Domain extends AppModel

public $validate = array(
    'name' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',


        //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    public $hasAndBelongsToMany = array(
        'Alignment' => array(
            'className' => 'Alignment',
            'joinTable' => 'alignments_domains',
            'foreignKey' => 'domain_id',
            'associationForeignKey' => 'alignment_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        ),
        'Domain' => array(
            'className' => 'Domain',
            'joinTable' => 'domains_domains',
            'foreignKey' => 'domain_id',
            'associationForeignKey' => 'domain_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        ),
        'Role' => array(
            'className' => 'Role',
            'joinTable' => 'roles_domains',
            'foreignKey' => 'domain_id',
            'associationForeignKey' => 'role_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        )
    );

}

But when i try to insert a new domain, it give me this error :

Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mythall_php`.`alignments_domains`, CONSTRAINT `alignments_domains_ibfk_2` FOREIGN KEY (`domain_id`) REFERENCES `domains` (`id`))

Do you have any suggestion on how I could make this possible ? Thanks !

i am new to cakephp.

i experience this type of errors. my mistake is i have added more row before seting foreign key.

try to empty those four tables in phpmyadmin (check the relationship constrains once again) add domain, alignment and do there.

and check this site for some reference : enter link description here

The solution I found was to change the hasandbelongtomany 'domain' to 'authorized_domain' and the associated foreign key to 'authorized_domain_id' and then baking was working properly.

'Authorized_Domain' => array(
            'className' => 'Domain',
            'joinTable' => 'domains_domains',
            'foreignKey' => 'domain_id',
            'associationForeignKey' => 'authorized_domain_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM