2014-09-20 3 views
0

내 DomainTypes view() 함수에 대한 내 DomainTypes 테이블의 하위 항목을 포함하려고하는데 "오류 : SQLSTATE [42S22] : 열을 찾을 수 없음 : 1054 알 수없는 열 '이라는 불편을 겪고 있습니다. Children.domain_type_id 'in'where clause ' "문제는 내 DomainTypes 테이블에 domain_type_id 열이 없다는 것입니다. 도메인 테이블 않습니다. 내 도메인 유형보기()에 대한 코드는 다음과 같습니다.연관 찾기 잘못된 테이블

public function view($id) { 
    if (!$id) { 
     throw new NotFoundException(__('Invalid DomainType')); 
    } 

    $domainType = $this->DomainTypes 
     ->find() 
     ->where(['DomainTypes.id' => $id]) 
     ->contain(['Parent', 'Children', 'Affiliates']) 
     ->first(); 

    $this->set(compact('domainType')); 
} 

내 설치에 대해 약간 설명합니다. 2 개의 테이블, 도메인 및 도메인 유형이 있습니다. 둘 다 Tree 비헤이비어를 사용합니다. 여기

는 도메인 표에 대한 초기화 코드 :

public function initialize(array $config){ 
    parent::initialize($config); 

    $this->displayField('name'); 
    $this->addBehavior('Tree'); 

    //Associations 
    $this->hasMany('Children', [ 
     'className' => 'Domains', 
    ]); 
    $this->belongsTo('Parent', [ 
     'className' => 'Domains', 
    ]); 
    $this->belongsTo('Affiliates'); 
    $this->belongsTo('DomainTypes'); 
} 

그리고 여기 DomainTypes 표에 대한 초기화 코드 :

public function initialize(array $config){ 
    parent::initialize($config); 

    $this->displayField('name'); 
    $this->addBehavior('Tree'); 

    //Associations 
    $this->hasMany('Children', [ 
     'className' => 'DomainTypes', 
    ]); 
    $this->belongsTo('Parent', [ 
     'className' => 'DomainTypes', 
    ]); 
    $this->belongsTo('Affiliates'); 
    $this->hasMany('Domains'); 
} 

매우 유사하지만 분명히 클래스 이름은 사용할 정의하는 두 . Cakephp 3에서 내 DomainTypes 테이블에 domain_type_id 열이 있다고 가정하는 이유는 무엇입니까? 미리 감사드립니다!

답변

0

업데이트 내가 제대로 관심을 지불하지되었고, CakePHP의 그것이 hasMany 협회 그래서 FK이 소스 테이블을 기반으로해야합니다, 그것은 domain_type_id라는 외부 키를 자동으로 생성 할 때 실제로 올바른 것 같다. 이 솔루션은 여전히 ​​동일

은 FK은 명시 적으로 설정되고, 이미 belongsTo 연관에 대해 @MjGaiser parent_id 의해 냈해야 필요는없고, 대신 FK은 hasMany 연관에 대해 parent_id를 사용하는 대신해야 child_id :

+0

내가 belongsTo 관계 만 사용한다면 의미가 있지만, hasMany는 childrens parent_id를 사용해야하기 때문에 "child_id"가되지 않아야합니다. parent_id와 child_id를 모두 갖고 있다면 데이터를 복제하고 사이트를 더 많은 문제로 여는 것입니다. 아니면 내가 어디로 가는지 이해하지 못하겠습니까? – MjGaiser

+0

@MjGaiser 아니, 네 말이 맞다. 제대로주의를 기울이지 않고'child_id'가 없어야하고'domain_type_id '라는 이름의 FK를 생성하는 CakePHP가 실제로 올바른 것으로 보인다. 내 대답을 업데이 트하고 말도 안되는 소리를 제거합니다 :) – ndm

0

우리는 모두 가깝지만 올바르지 않습니다. ndm, 당신의 대답은 도움이되었지만 내가 한 또 다른 실수를 지적했습니다. 마지막 연결 코드는 다음과 같아야합니다

$this->hasMany('Children', [ 
     'className' => 'DomainTypes', 
     'foreignKey' => 'parent_id', 
    ]); 
    $this->belongsTo('Parent', [ 
     'className' => 'DomainTypes', 
    ]); 

내가 외국 키를 입력하려고 때, 대신 "외래 키", 나는 거의 쓸모가 없었습니다 "foreign_key"을 사용했다. 도움 ndm 주셔서 감사합니다.