Cakephp 3:如何获取 joing table 关联数据?
Cakephp 3 : How to fetch joing table associated data?
我有一个table名字countries
,countries
和users
有关系,users
和MoneyTransferTransactions
有关系
在 MoneyTransferTransactions
视图中,我需要获取国家名称。
我已经通过下面的代码
在 MoneyTransferTransactionsTable
中加入用户 table
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
在用户 table 中,我也像下面的代码一样使用内部连接
$this->belongsTo('Countries', [
'foreignKey' => 'country_id',
'joinType' => 'INNER'
]);
在 MoneyTransferTransactions 控制器中,我使用以下代码获取所有数据和相关数据。
$this->paginate = [
'contain' => ['Users','TransferOptions'],
'conditions'=> ['MoneyTransferTransactions.status'=>1],
'order' =>['MoneyTransferTransactions.id'=>'DESC']
];
我在 index.ctp 中使用了 var_dump ,我从 users
table 获得了国家 ID,但没有从国家 table 获得国家名称。如何从 MoneyTransferTransactions>index.ctp
获取国家/地区名称?
在'contain'
中添加用户和国家之间的关联
$this->paginate = [
'contain' => ['TransferOptions', 'Users' => ['Countries']],
'conditions' => ['MoneyTransferTransactions.status' => 1],
'order' => ['MoneyTransferTransactions.id' => 'DESC']
];
我有一个table名字countries
,countries
和users
有关系,users
和MoneyTransferTransactions
有关系
在 MoneyTransferTransactions
视图中,我需要获取国家名称。
我已经通过下面的代码
在MoneyTransferTransactionsTable
中加入用户 table
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
在用户 table 中,我也像下面的代码一样使用内部连接
$this->belongsTo('Countries', [
'foreignKey' => 'country_id',
'joinType' => 'INNER'
]);
在 MoneyTransferTransactions 控制器中,我使用以下代码获取所有数据和相关数据。
$this->paginate = [
'contain' => ['Users','TransferOptions'],
'conditions'=> ['MoneyTransferTransactions.status'=>1],
'order' =>['MoneyTransferTransactions.id'=>'DESC']
];
我在 index.ctp 中使用了 var_dump ,我从 users
table 获得了国家 ID,但没有从国家 table 获得国家名称。如何从 MoneyTransferTransactions>index.ctp
获取国家/地区名称?
在'contain'
中添加用户和国家之间的关联$this->paginate = [
'contain' => ['TransferOptions', 'Users' => ['Countries']],
'conditions' => ['MoneyTransferTransactions.status' => 1],
'order' => ['MoneyTransferTransactions.id' => 'DESC']
];