簡體   English   中英

CakePHP兩次加入同一模型

[英]CakePHP Joining same model twice

我有以下表格:

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

你對我如何做到這一點有什么建議嗎? 謝謝 !

我是cakephp的新手。

我遇到過這種錯誤。 我的錯誤是我在設置外鍵之前添加了更多行。

嘗試在phpmyadmin中清空這四個表(再次檢查關系約束)添加域,對齊並執行此操作。

並查看此站點以供參考: 在此輸入鏈接描述

我找到的解決方案是將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' => '',
        )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM