我正在尝试在两个表,帐户和转移之间建立关联。在转账表格中,用户应该能够选择原始账户以及转账的目的地账户。但是在页面中,第一个列表(原始帐户)为空,只有第二个(目标帐户)具有现有帐户列表。
我需要的是原始帐户列表也显示数据。
我已经阅读了“关联 - 将表连接在一起”文档的页面并在堆栈溢出中搜索了这些信息,但我遗漏了一些东西。你能帮忙吗?提前谢谢。
我的表帐户和转帐构建如下:
CREATE TABLE accounts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
account_types_id INT UNSIGNED,
user_id INT UNSIGNED NOT NULL,
description TEXT,
created DATETIME,
modified DATETIME,
FOREIGN KEY user_key (user_id) REFERENCES users(id),
FOREIGN KEY account_types_key (account_types_id) REFERENCES account_types(id),
UNIQUE KEY name_user(name, user_id)
);
和
CREATE TABLE transfers (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id INT UNSIGNED NOT NULL,
origin_account_id INT UNSIGNED NOT NULL,
destination_account_id INT UNSIGNED NOT NULL,
value decimal(10,2) NOT NULL,
description TEXT,
created DATETIME,
modified DATETIME,
FOREIGN KEY user_key (user_id) REFERENCES users(id),
FOREIGN KEY origin_account_key (origin_account_id) REFERENCES accounts(id),
FOREIGN KEY destination_account_key (destination_account_id) REFERENCES accounts(id)
);
我的模特如下:
TransfersTable.php:
$this->belongsTo('Accounts', [
'className' => 'Accounts',
'foreignKey' => 'origin_account_id',
'propertyName' => 'account',
'joinType' => 'INNER'
]);
$this->belongsTo('Accounts', [
'className' => 'Accounts',
'foreignKey' => 'destination_account_id',
'propertyName' => 'account',
'joinType' => 'INNER'
]);
AccountsTable.php:
$this->hasMany('Transfers', [
'className' => 'Accounts',
'foreignKey' => 'account_id',
'bindingKey' => 'origin_account_id'
]);
$this->hasMany('Transfers', [
'className' => 'Accounts',
'foreignKey' => 'account_id',
'bindingKey' => 'destination_account_id'
]);
答案 0 :(得分:0)
解决方案是在Template / Transfers / add.ctp中。就像这样:
echo $this->Form->input('origin_account_id');
echo $this->Form->input('destination_account_id', ['options' => $accounts]);
应该是这样的:
echo $this->Form->input('origin_account_id', ['options' => $accounts]);
echo $this->Form->input('destination_account_id', ['options' => $accounts]);