cakephp - Cakephp 如何映射和合并两个对象?
问题描述
我有两个对象coins
和user_summarry
$coins = $this->coins->find('all')
$user_summarry = $this->UserSummary->find('all')
如果我toArray()
用于硬币,它看起来像
[
(int) 0 => object(App\Model\Entity\Coin) id:0 {
'id' => (int) 1
'name' => 'A'
}
(int) 1 => object(App\Model\Entity\Coin) id:0 {
'id' => (int) 2
'name' => 'B'
}
....
]
toArray()
为了user_summarry
[
(int) 0 => object(App\Model\Entity\UserSummary) id:0 {
'id' => (int) 1
'coin_id' => '2'
'total' => 120
}
...
]
我怎样才能改变我的硬币对象
(int) 0 => object(App\Model\Entity\Coin) id:0 {
'id' => (int) 1
'name' => 'A',
'total' => 0
}
(int) 1 => object(App\Model\Entity\Coin) id:0 {
'id' => (int) 2
'name' => 'B',
'total' => 120
}
通过使用 2 foreach 循环更改数组后,我可以将其更改为数组。如何使用 cakephp 方法映射和连接为对象?
解决方案
小建议:
硬币表:
$this->hasOne('UserSummary',[
'foreignKey' => 'UserSummary.coin_id',
];
在您的控制器中:
$results = $this->Coins->find()
->select(['id' => 'Coins.id', 'name' => 'Coins.name', 'total' => 'UserSummary.total'])
->toArray();
debug($results);
推荐阅读
- json - JSON路径条件似乎永远不会工作 - 不确定为什么
- java - 在 Kotlin 中解析列表的枚举
- powershell - 如何从多个文件中获取平均 lastwritetime?
- python - 如何在 Whoosh 中使用 WordNet 词库
- git - 需要一个仅提供我的源分支(工作分支)更改的 git 命令
- ruby-on-rails - 尝试登录我的网站时,我不断收到:2 个错误禁止保存此用户:电子邮件不能为空白,电子邮件不能为空白
- jquery - 附加元素中的删除按钮不起作用
- java - 使用重试模板执行自定义重试策略
- scheme - 使用方案反转一般列表
- bash - Bash Printf 颠倒了参数的顺序