2017-11-14 16 views
1

私はのテーブルを持っています。idのカラムを参照する必要があるカラムtagging_idは、tracking_categoriesを外部キーとして参照しています。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は、次のようになります。私はAppModelArticle.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を取得していますエラーをollowing: 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()'コールバックから呼び出すことができます。 –

関連する問題