CakePHP两次加入同一模型

时间:2015-03-05 04:18:31

标签: php mysql sql cakephp model

我有以下表格:

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

我的所有外键都已制作好,而我想要实现的目标是为每个域设置多个授权域和几个授权路线。

事情是,当我把蛋糕烘烤模型,控制器和视图我有这个模型时:

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' => '',
        )
    );

}

但是当我尝试插入新域名时,它会给我这个错误:

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`))

您对我如何才能做到这一点有什么建议吗? 谢谢!

2 个答案:

答案 0 :(得分:0)

我是cakephp的新手。

我遇到了这种类型的错误。我的错误是我在设置外键之前添加了更多行。

尝试在phpmyadmin中清空这四个表(再次检查关系约束) 添加域,对齐并执行此操作。

并查看此网站以获取一些参考:enter link description here

答案 1 :(得分:0)

我找到的解决方案是改变hasandbelongtomany'域名'到'authorized_domain'和'authorized_domain_id'的关联外键然后烘烤工作正常。

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