【问题标题】:Cakephp 3 : How to fetch joing table associated data?Cakephp 3:如何获取连接表关联数据?
【发布时间】:2016-12-07 10:48:53
【问题描述】:

我有一个表名countriescountriesusers有关系,usersMoneyTransferTransactions有关系

MoneyTransferTransactions 视图中,我需要获取国家/地区名称。

我已经通过下面的代码加入了MoneyTransferTransactionsTable中的用户表

$this->belongsTo('Users', [
                'foreignKey' => 'user_id',
                'joinType' => 'INNER'
]);

在用户表中,我还使用了如下代码的内连接

$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 表中获得了国家 ID,但没有从国家表中获得国家名称。如何从MoneyTransferTransactions>index.ctp 获取国家名称?

【问题讨论】:

  • 你必须添加国家来包含
  • 它给了我错误“MoneyTransferTransactions 与国家没有关联”
  • 添加 Users.Countries 以包含并从中删除用户

标签: cakephp cakephp-3.0 cakephp-3.x


【解决方案1】:

在“包含”中添加用户和国家之间的关联

$this->paginate = [
    'contain' => ['TransferOptions', 'Users' => ['Countries']],
    'conditions' => ['MoneyTransferTransactions.status' => 1],
    'order' => ['MoneyTransferTransactions.id' => 'DESC']
];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多