2017-11-14 24 views
1

tracking_categories 테이블의 id 열을 외래 키로 참조해야하는 tagging_id 열이있는 articles 테이블이 있습니다.CakePHP가 foreignKey 이름을 준수하지 않습니다

Article.php 파일은 다음과 같습니다

class Article extends AppModel { 
    public $controllerPath = 'articles'; 
    public $useTable = 'articles'; 

var $binds = array(
    'TrackingCategory' => array(
     'bindType' => 'belongsTo', 
     'className' => 'TrackingCategory', 
     'foreignKey' => 'tagging_id' 
    ), 
    'Channel' => array(
     'bindType' => 'belongsTo', 
     'className' => 'Channel', 
     'foreignKey' => 'channel_id' 
    ) 
); 

TrackingCategory.php은 다음과 같습니다 : I이 (가) AppModel하는 Article.php 상속에 다음 한

class TrackingCategory extends AppModel { 

    public $useTable = 'tracking_categories'; 

:

function expects($binds, $reset = false) { 
    if (func_num_args() > 2 || !is_bool($reset)) { 
     throw new InvalidArgumentException('Did you mean to pass an 
array of binds?'); 
    } 

    if (!is_array($binds)) { 
     $binds = array($binds); 
    } 

    foreach ($binds as $bind) { 

     if (isset($this->binds[$bind])) { 
      $tmp = array($this->binds[$bind]['bindType'] => array($bind => $this->binds[$bind])); 
      $this->bindModel($tmp, $reset); 
     } 
    } 
} 

I f를 얻고있다. 따르게 오류 : Database Error Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Article.tracking_category_id' in 'field list'

아직 CakePHP의 명명 규칙이 아니라 내가 명시 적으로 foreignKey 값을 통해 설정 한 이름을 통해 분석이라는 이름의 열을 찾고있다. 내가 여기서 무엇을 놓치고 있니?

답변

0

오른쪽 구문은 다음과 같아야

자세한 내용
public $belongsTo = array(
    'TrackingCategory' => array(
     'className' => 'TrackingCategory', 
     'foreignKey' => 'tagging_id', 
    ) 
); 

CakePHP 2.x - Associations: Linking Models Together - belongsTo 참조.

+0

대체 구문은이 새로운 모델이 아닌 해당 모델의 다른 모든 바인딩에 대해 잘 작동합니다. 이것은 조직을위한 기존 프로젝트입니다. 작업 구문을 바꿀 수는 없습니다. –

+0

답장을 보내 주셔서 감사합니다. 묶여 있고 올바르게 작동하는 채널을 포함하도록 질문을 편집했습니다. 유일한 차이점은 channel_id가 CakePHP의 명명 규칙을 따르고 있다는 것입니다. –

+0

다른 작업 모델에서'$ this-> bindModel ($ this-> binds);'같은 코드를 찾아보십시오. 그것은 생성자 또는'beforeFind()'콜백 내에서 호출 될 수 있습니다. –