0

私はここで私がやりたいことをCakeにほとんどやらせましたが、完全ではありません。それは私の知識にギャップがあるからだと思います.

英国の郵便番号のデータベース テーブルを CakePHP アプリにインポートしました。構造は次のとおりです。

CREATE TABLE IF NOT EXISTS `postcodes` (
  `ref` varchar(6) NOT NULL DEFAULT '',
  `area` varchar(50) NOT NULL DEFAULT '',
  `uk_region` varchar(4) NOT NULL,
  `lat` decimal(6,4) NOT NULL DEFAULT '0.0000',
  `long` decimal(5,4) NOT NULL DEFAULT '0.0000',
  PRIMARY KEY (`ref`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

そのテーブルの行を CSV 形式で示します。

"AB10","Aberdeen","SCOT","57.1350","-2.1170"

「アカウント」と「注文」は、「郵便番号参照」からこれらの詳細を検索できる必要があります

それで、この記事http://www.visuallizard.com/blog/2009/02/19/210を読んだ後、私はこれを思いつきました(アカウントモデルのみを示します):

class Account extends AppModel {
    public $hasOne = array('Postcode' => 
        array(
          'className' => 'Postcode', 
          'finderQuery' => 'SELECT Postcode.* FROM accounts, postcodes AS Postcode WHERE accounts.id = {$__cakeID__$} AND accounts.postcode_ref = Postcode.ref', 'foreignKey' => false
));

}

ここで、「16」がテスト アカウント ID であるこれらのいずれかを実行すると、次のようになります。

$this->Account->read(null, 16);
$this->Account->find('first', array('conditions' => array('Account.id' => 16)));

データを取得するには、すべて問題ありません。しかし、もしそうなら:

$this->Account->find('all', array('conditions' => array('Account.id' => 16)));

正しい結果の配列が得られますが、2,821 回です。これは、郵便番号エントリの数です。

$hasOne から $hasMany に変更しても、結果は 1 回だけ返されますが、すべての hasMany クエリがそうであるように、$result['Postcode'][0] 内にあり、あなたの何人かが確信しているように、それは私の芯にかかるでしょう。わかるかもしれません。

私がここで何をしたかについての手がかりはありますか?私は何かを誤解していますか、それともこれは CakePHP のバグですか?

4

1 に答える 1

3

あなたの最善の選択肢は、関係を「変える」ことです。アカウントは郵便番号に属しています。アカウントは単一の郵便番号しか持つことができないため、基本的にそれは郵便番号に「属し」、各郵便番号 (エリア) は複数のアカウントを持つ (含む) ことができます。

アカウント テーブルのforeignKeyフィールドに適切な名前を付けたように見えますが、郵便番号モデル内の主キーである「ref」を必ず指定してください。関係は次のようになります。

Account extends AppModel {
    public $belongsTo = array(
        // additional settings are probably not
        // required because postcode_ref follows the
        // CakePHP conventions, so foreignKey will
        // automatically be detected
        'Postcode',
    );

}

郵便番号モデル:

Postcode extends AppModel {
    // Important because of non-standard PK name
    public $primaryKey = 'ref';


    public $hasMany = array(
        'Account',
    );
}

これはおそらくうまくいくはずです

于 2013-03-15T20:29:07.067 に答える